Home > Software design >  Write a programme to input 10 numbers from the user and print greatest of all
Write a programme to input 10 numbers from the user and print greatest of all

Time:01-20

I have written the programme and there's some kind of coding mistake I guess. The inputting part in working properly but finding and printing greatest number isn't.

#include<stdio.h>
int main()
    {
    int i, arr[10],c=0;
    for(i=1;i<=10;i  )
    {
        printf("enter 10 nos. for arr[%d] :",i);
        scanf("%d",&arr[i]);
    }
    for(i=1;i<=10;i  )
    {
        while(arr[i]>c)
        {
            c=arr[i];
        }
        printf("Greatest number in a given array is:%d",c);
    }
    return 0;
} 

CodePudding user response:

In C indexes start from 0, so for(i=1;i<=10;i ) condition is wrong in two ways: you're starting from the second array item (missing arr[0]), then you're going out of bounds with arr[i] when i is 10.

You should change the while to if as well, since you don't actually intend to have a loop there. You only need to execute the code under the condition once. You should also consider what happens when you have negative values in your input.

Last but not least - the printf probably should be outside the for loop if you want to print out the end result.

CodePudding user response:

If you have an array declared like

int a[N];

where N is some positive integer value then the valid range of indices to access elements of the array is [0, N).

It means that for example this for loop

for(i=1;i<=10;i  )
{
    printf("enter 10 nos. for arr[%d] :",i);
    scanf("%d",&arr[i]);
}

must look like

for ( i = 0; i < 10; i   )
{
    printf("enter 10 nos. for arr[%d] :",i);
    scanf("%d",&arr[i]);
}

This while loop

    while(arr[i]>c)
    {
        c=arr[i];
    }

jus does not make a sense an can be an infinite loop.

Moreover this call of printf

    printf("Greatest number in a given array is:%d",c);

is placed within a for loop.

The program can look the following way

#include <stdio.h>

int main( void )
{
    enum { N = 10 };
    int arr[N];

    printf( "Enter %d numbers:\n", N );
 
    for ( int i = 0; i < N; i   )
    {
        printf("\t%d: ", i   1 );
        scanf( "%d", arr   i );
    }

    int max = 0;

    for ( int i = 1; i < N; i   )
    {
        if ( arr[max] < arr[i] ) max = i;
    }

    printf( "The greatest number in the given array is: %d\n", arr[max] );

    return 0;
} 

CodePudding user response:

First, index of array starts with zero and arr[10] is out of bound.
Second, assign the minimum integer value to c.
Third, change while to if and put the second printf() out of second for statement.

CodePudding user response:

Here is the Code

#include <stdio.h>
  int main() {
    // Taking Variables
    int a[10];
    int i;
    int greatest;

    //Taking exactly 10 inputs in an array by using for loop
    printf("Enter ten values:");
    for (i = 0; i < 10; i  ) {
      scanf("%d", &a[i]);
    }

    //Iterating through the array and comparing all the values, the one which is the greatest of all is printed
    greatest = a[0];
    for (i = 0; i < 10; i  ){
      if (a[i] > greatest){
        greatest = a[i];}
      }
    printf("Greatest of ten numbers is %d", greatest);
    return 0;
  }

CodePudding user response:

This should work

#include<stdio.h>
int main()
    {
    int i, arr[10],c=0;
    for(i=1;i<=10;i  )
    {
        printf("enter 10 nos. for arr[%d] :",i);
        scanf("%d",&arr[i]);
    }
    for(i=1;i<=10;i  )
    {
        if(arr[i]>c)
        {
            c=arr[i];
        }
    }
    printf("Greatest number in a given array is:%d",c);
    return 0;
}
  •  Tags:  
  • Related