0

So I have this code where I make the user enter between how many numbers does thy want to make the comparation:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void){
    int n;
    int x;
    int numbers[n];
    int count;

    printf("Mitme arvu vahel soovite võrdlust sooritada?: ");
    scanf("%d",&n);
    printf("Soovisite võrdlust teostada %d arvu vahel\n",n);
    printf("Sisestage palun arvud: \n");
    for (count = 1; count <= n; count++ ){
        printf("Arv %d:",count);
        scanf("%d",&numbers[n]);
        }
    for (count = 0; count <= n; count++){
        if (numbers[count] > x){
            x = numbers[count];
            }
        }
    printf("%d\n",x);
    return 0;
    }

Now the problem with it is that when the final printf is made, I get somesort of unrealistic number.

This is the output when I make the n 3:

./ComparingC Mitme arvu vahel soovite võrdlust sooritada?: 3
Soovisite võrdlust teostada 3 arvu vahel
Sisestage palun arvud: 
Arv 1:1
Arv 2:2
Arv 3:3
4196269

The number at the end is what I am talking about. It should show the biggest number amongst all of the other number but right now it showing something out of the deep space it seems.

EDIT: Also when I make the n equal 5 and enter 1 as the first number, the program ends right there?

Kaspar
  • 1,600
  • 4
  • 24
  • 46

5 Answers5

2

You are completely skipping first element in your array!

 for (count = 1; count <= n; count++ ){
    scanf("%d",&numbers[n]);

Start from 0th index and go till n-1

 for (count = 0; count < n; count++ ){

PS: value of n is to be initialized.

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
2

You declare numbers[n] without setting n to a sensible value. You can use variable-length arrays in C99, but your length is undefined which isn't a good idea.

Move the array declaration to after n has a proper value. And verify that the scanf() works before relying on the value.

Also, arrays in C are indexed from 0, so the loop is wrong too. It should be:

for (count = 0; count < n; ++count)

Further, the loop doesn't use count, it should be:

scanf("%d", &numbers[count]);

inside the loop, not &numbers[n] (which is an invalid out-of-bounds access that will invoke undefined behavior).

unwind
  • 391,730
  • 64
  • 469
  • 606
2

Error 1: n is not initialized to proper value before declaring numbers[n]. n will be containing some garbage value.

Error 2: x is not initialized before using it in if (numbers[count] > x).

I don't think I need to tell how to correct these logical errors.

0xF1
  • 6,046
  • 2
  • 27
  • 50
1

You are using variable length array (C99 feature). The value of n must be initialized before declaring VLA. Move your declaration

int numbers[n];  

after inputting n, i.e, after the statement scanf("%d",&n);

 scanf("%d",&n);
 int numbers[n];  
haccks
  • 104,019
  • 25
  • 176
  • 264
1

This is what you want.

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int n;
    int x = 0;
    int* numbers=NULL;
    int count;

    printf("Mitme arvu vahel soovite võrdlust sooritada?: ");
    scanf("%d",&n);
    printf("Soovisite võrdlust teostada %d arvu vahel\n",n);

    numbers = malloc(sizeof(int) * n);
    if(numbers == NULL)
         return -1;

    printf("Sisestage palun arvud: \n");
    for (count = 0; count < n; count++ ){
        printf("Arv %d:",count+1);
        scanf("%d",&numbers[count]);
        }
    for (count = 0; count <= n; count++){
        if (numbers[count] > x){
            x = numbers[count];
            }
        }
    printf("%d\n",x);
    return 0;
    }

Just run it at your machine and find out your mistakes in your code and learn.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222