How to scan user input values(integers) and store them into an array?

This is a discussion on How to scan user input values(integers) and store them into an array? within the C Programming forums, part of the General Programming Boards category; I am trying to create a function that asks the user to enter a set of 10 numbers. This function ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    How to scan user input values(integers) and store them into an array?

    I am trying to create a function that asks the user to enter a set of 10 numbers.

    This function should fill my array with the values entered. The trick is doing it by using only functions and calling the functions inside main.

    the function should have an integer array as its formal parameter.

    Do I have to set a constant SIZE of the array to 10, since I want the user to input exactly 10 numbers?

    Also where would I declare the array, inside main, or the function?


    my prototype is currently set to this:

    Code:
    int getnumbers(int , int);
    in the main I try calling getnumbers, but i get errors. I honestly am confused on how to scan the user input values and store them into the array.



    and my function is this:

    Code:
    int getnumbers(int size, int input)
    
    {
    	printf("please enter your list of numbers: ");
    	scanf( "%d", &input);
    
    return input;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by x3ldonx
    the function should have an integer array as its formal parameter.
    You cannot actually do that. Rather, the formal parameter can be a pointer to an element of the array, i.e., the first element of the array.

    Quote Originally Posted by x3ldonx
    Do I have to set a constant SIZE of the array to 10, since I want the user to input exactly 10 numbers?
    That is a sensible thing to do.

    Quote Originally Posted by x3ldonx
    Also where would I declare the array, inside main, or the function?
    In the main function.

    Quote Originally Posted by x3ldonx
    and my function is this:
    As I mentioned earlier, you should have a pointer as one of the parameters. You can omit the size parameter since it is constant, though including it would make the function more reusable. input should be a local variable rather than a parameter, and you will likely need a loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    My Function reads this, but it is only storing the values in the array I have created in the function. How can I send it from one function to another, or at least pass it to the main function?

    What I am trying to do now, is find the largest and smallest values in the array in a new function.

    Code:
    void GetNumbers( int [] )
    {
    	int b[ 10 ];
    	int number = 0;
    	
    	printf( "Please enter your list of ten numbers: " );
    
    	for ( int i = 0 ; i < 10; i++ ) {
    		scanf( "%d", &number );
    		b[ i ] = number;
    	}
    
    	printf( "Your array has these values: " );
    
    	for ( int i = 0 ; i < 10; i++ ) {
    		printf( "%d ", b[ i ] );
    	}
    
    	printf( "\n" );
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Why am I getting 0 as the largest value?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void Instruct( void );
    void GetNumbers( int [] );
    int FindLargest (int []);
    
    void main()
    {
    	int a[ 10 ];
    
    	Instruct();
    	GetNumbers( a );
    
    	printf("the largest value is %d\n", FindLargest(a));
    	
    	
    	system( "pause" );
    }
    
    void Instruct( void ) 
    {
    	printf( "This program will determine the largest and smallest values in a set of ten numbers. "
    	"It will also compute the sum and average of the numbers.\n\n" );
    }
    
    void GetNumbers( int [] )
    {
    	int b[ 10 ];
    	int number = 0;
    	
    	printf( "Please enter your list of ten numbers: " );
    
    	for ( int i = 0 ; i < 10; i++ ) {
    		scanf( "%d", &number );
    		b[ i ] = number;
    	}
    
    	printf( "Your array has these values: " );
    
    	for ( int i = 0 ; i < 10; i++ ) {
    		printf( "%d ", b[ i ] );
    	}
    
    	printf( "\n" );
    }
    
    int FindLargest ( int array [] )
    
    {
    	
    	int maxNumber = 0 ;
    	for(int i = 0; i < 10; i++)
    	{
    		if(array[i] > maxNumber)
    	maxNumber = array[i];
    	}
    	return maxNumber;
    	}

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21