-1

I've been asked to write a program that accepts a list of numbers until a non-numeric is encountered (up to 30 numbers), putting the numbers into an array, and keeping track of how many numbers were inserted. Then it should scan through the array to find the largest number, and print the largest.

This is what I've come up with:

#include<stdio.h>
int main()

{
    const int INPUT = 30 ;
    int size [INPUT];
    int i, big;


  printf("Type integer numbers, followed by q to quit: ");
  while (scanf("%d", &size[INPUT]) != 'q')
 {

    for(i=0;i<size;i++)
      scanf("%d",&INPUT[i]);
  big = INPUT[0];


  for(i=1;i<size;i++)

  {
      if(big<INPUT[i])
           big=INPUT[i];
  }

  printf("The largest number is %d",big);
  return 0;
}
jwrush
  • 743
  • 7
  • 25
Yvantc
  • 3
  • 3

2 Answers2

1

Besides the problems, I listed in the comments. You seems to be comfused by the varaible names~ Anyway, I made some code for you.

#include<stdio.h>

int main()
{
    const int MAX_INPUT = 30 ;
    int input[MAX_INPUT];
    int size=0, big;


    printf("Type integer numbers, followed by q to quit: ");
    while(size < MAX_INPUT){
        if(scanf("%d", &input[size]) != 1){
            break;
        }
        ++size;
    }

    if(size ==0){
        return 0;
    }

    big = input[size-1];

    while( size-- > 0)
    {
        if(big<input[size]){
            big=input[size];
        }
    }

    printf("The largest number is %d\n",big);
    return 0;
}

Tested with GCC 4.1.2 and Linux.

Sheng
  • 3,467
  • 1
  • 17
  • 21
  • thank you very very very much, now i will work on this again and try to figure how to work the while loop so i can do it over and over again – Yvantc Apr 02 '13 at 01:25
  • ok another this is when i enter q alone instead of quitting is giving me a value as maximum, and the while loop is not looping. – Yvantc Apr 02 '13 at 01:37
  • My fault. Forget to check it. I updated the code. – Sheng Apr 02 '13 at 01:43
0

Return value of scanf:

Upon successful completion, these functions return the number of successfully matched and assigned input items

further, you are mixing the size and input, you actually want the size to be a constant and input to be an array:

const int SIZE = 30 ;
int input[SIZE];

So the while loop should look like:

while (scanf("%d", &input[some_index]) == 1)

and of course this is wrong:

scanf("%d",&INPUT[i]);  // should be ==> &input[i]
perreal
  • 94,503
  • 21
  • 155
  • 181
  • but the reason y i put q is because 1st is an array so i will be entering multiple numbers, and i want it to stop when i enter q. – Yvantc Apr 02 '13 at 01:02
  • q will stop the loop because it cannot be successfully assigned to an integer. – perreal Apr 02 '13 at 01:04