-2

I have a problem with my code. It should take from user amount of numbers in array and user should write the number he wants in range 1-100.

Also the program should display the smallest and the highest number. If I want to have an array with more than 7 numbers, it crashes down while I type in the numbers.

Every time when it comes to the number 7, program crashes. I have no idea why it isn't working.

#include <stdio.h>
#include <stdlib.h>
int n, i, a;
int main ()
{
 int tab[n];
 printf("\nhow many elements you want to have?\n");
 do
 {
 scanf ("%d", &n);
 if ((n>30 || n<1))
 printf("\ntoo high or too low\n"); 
 }
 while ((n<1 || n>30));
 printf("\nyour number please:\n");
 for(i = 0; i < n; i++)
 {
 do
 {
 printf("\nelement %d:", i+1);
 scanf("%d", &a);
 if ((a < 1 || a > 100))
 printf("\nnumber too high or too low\n");
 tab[i]=a;
 }
 while((a < 1 || a > 100));
 }
 printf("\nyour numbers:");
 for (i=0; i<n; i++)
 {
 printf("\n%d", tab[i]);
 }
 int min = tab[0];
 int max = tab[0];
 for (i = 0; i<n; i++)
 {
 if (tab[i]> max)
 max = tab[i];
 if(tab[i]< min)
 min = tab[i];
 }
 printf("\nsmallest: %d", min);
 printf("\nbiggest: %d", max);
}
HDJEMAI
9,84048 gold badges78 silver badges99 bronze badges
asked Jan 19, 2016 at 1:38
2
  • int tab[n]; <- tell me, what does n contain when the computer executes this line? Commented Jan 19, 2016 at 1:53
  • Enasble compiler warnings! Then read them, understand them and fix your code. Commented Jan 19, 2016 at 2:03

3 Answers 3

1

The problem here is this declaration:

int tab[n];

You are declaring a variable length array (available since C99) with an uninitialized value as the length. I understand that you want to make room for the numbers that you are going to store from stdin but that's not the proper way to do it.

I see two solutions:

  • the easy one, choose a maximum fixed amount of numbers (which is the case in your situation since you allow a maximum of 30 values) and change the declaration to int tab[30]
  • use dynamically allocated memory, for example take a look here
answered Jan 19, 2016 at 1:44
Sign up to request clarification or add additional context in comments.

1 Comment

Global variables are initialized to zero, so it declares an array with 0 length.
0

If you add printf("%d",sizeof tab); after the declaration int tab[n], the program will output 0.

You wrote int n; outside main(), making it a global variable, which is initialized to zero by default, but you didn't give it any other value before using it in the statement int tab[n];, so it's equal to int tab[0] in your case.

To use VLA, you must declare tab[n] after n is given by the user.

This error causes undefined behavior, allowing anything to happen. Thus, your program can crash when you input the second number, or it will never crash, if you are really (un)lucky. Number 7 doesn't have any magic power.

answered Jan 19, 2016 at 1:54

Comments

-1

you have to specife your array length in C

tab[100];
answered Jan 19, 2016 at 1:46

1 Comment

Please do some research and learn about variable length arrays before writing factually incorrect answers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.