Thread: displaying user input values of an array by calling functions... PLEASE HELP ME!!!

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    Question displaying user input values of an array by calling functions... PLEASE HELP ME!!!

    Hi, i'm new to this forum and i have seen a lot of really useful information given here, so i thought i would give it a shot.

    I'm doing a program that is VERY basic and the spec of the program calls for me to have the user input a certain amount of values and I have to display the smallest of the values, the largest of the values and then i have to display the array itself with each value the user input. Each has to be done by main() calling each of these as a separate function. finding and displaying the smallest and largest were fairly easy, but when i get to displaying the array, it displays the number of elements in the array, without the corresponding values...

    PLEASE HELP ME!!! THIS HAS ME SO STUMPED!

    here's a copy of the program

    Code:
     #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 5
    
    
    int largest(int L[]){ 
    	int i,big;
    
    	big=L[0];
    	for (i=1;i<SIZE;i++){
    		if (L[i]>big){
    			big=L[i];
    		}
    
    	}	return big;
    }
    
    
    
    int smallest(int S[]){
    	int i, small; 
    
    	small=S[0];
    	for (i=1;i<SIZE;i++){
    		if (S[i]<small){
    			small=S[i];
    		}
    
    	}return small;
    }
    
    
    
    void displayArray(){
    	int i;
    
    
    	printf("\nHere is your array:\n\n");
    	printf("You have %i elements in this array.\n",SIZE);
    	for (i=0;i<SIZE;i++){
                 //this is where i get stuck!!
    		printf("In element [%i] is the value %i \n",i+1); //what goes here
    	}
    
    	return i;
    }
    
    main (){
    
    	int large,i,small,finish;
    	int element[SIZE];
    
    	do{
    
    		for (i=0;i<SIZE;i++){
    			printf("Enter value for element [%i]: ",i+1);
    			scanf("%i",&element[i]);
    		}
    
    
    		large=largest(element);
    		small=smallest(element);
    		displayArray();
    
    
    		printf("\nthe largest value is: %i\n",large);
    		printf("the smallest value is: %i\n\n",small);
    
    		printf("press 1 to quit: ");
    		scanf("%i",&finish);
    
    		system ("pause");
    	}
    
    	while (finish!=1);
    }

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    printf("The element [%i] of the array is %i\n", i + 1, elements[i]);

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    am i doing that in main() or displayArray? When I do it in displayArray it tells me I have an unidentified variable. Then when i declare the variable it still doesn't show the values that the user input. Is there more that needs to go into the displayArray() function?

    if it's in main() it only shows one element and a garbage number

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to pass your array as a parameter to the displayArray function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
     #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 5
    
    
    int largest(int *L){ 
    	int i,big;
    
    	big=L[0];
    	for (i=1;i<SIZE;i++){
    		if (L[i]>big){
    			big=L[i];
    		}
    
    	}	return big;
    }
    
    
    
    int smallest(int *S){
    	int i, small; 
    
    	small=S[0];
    	for (i=1;i<SIZE;i++){
    		if (S[i]<small){
    			small=S[i];
    		}
    
    	}return small;
    }
    
    
    
    void displayArray(int *Array) {
    	int i;
    
    	printf("\nHere is your array:\n\n");
    	for (i=0;i<n;i++) {
                //this is where i get stuck!!
                printf("In element [%i] is the value %i \n",i; Array[i]);
    	}
    	printf("You have %i elements in this array.\n",SIZE);
    }
    
    main (){
    
    	int large,i,small,finish;
    	int element[SIZE];
    
    	do{
    
    		for (i=0;i<SIZE;i++){
    			printf("Enter value for element [%i]: ",i+1);
    			scanf("%i",&element[i]);
    		}
    
    
    		large=largest(element);
    		small=smallest(element);
    		displayArray(element);
    
    
    		printf("\nthe largest value is: %i\n",large);
    		printf("the smallest value is: %i\n\n",small);
    
    		printf("press 1 to quit: ");
    		scanf("%i",&finish);
    
    		system ("pause");
    	}
    
    	while (finish!=1);
    }
    I haven't run it, but that should do it. You weren't passing the array name (which becomes a pointer to the array when it's being passed to a function, thus the *), to DisplayArray. Having no knowledge of what the array was, it printed up garbage.

    If you use prototypes for your functions, it's easier to spot these kinds of problems.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Code:
    void displayArray(int /*what goes here*/[]){
    
    	int i;//what goes here
    
    
    	printf("\nHere is your array:\n\n");
    	printf("You have %i elements in this array.\n",SIZE);
    	for (i=0;i<SIZE;i++){
    		printf(" In element [%i] is the value %i \n",i,(/*what goes here*/));
    		
    	}
    
    	return i;
    this is what i've got

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    the number of elements in the array are being displayed just fine, however the value that the user inputs in each element of the array is what i want to display but it won't...
    for instance.

    //I would display //user would input
    "please enter value for element[1]: 6
    "please enter value for element[2]: 2
    "please enter value for element [3]: 1

    //then i would display
    "for element [2] the value is 2
    "for element [3] the value is 1
    "for element [1] the value is 6

    i can display the number of elements, but the values are my issue.

    i really hope that was clear...i have a hard time explaining stuff typing
    Last edited by hiddenprophecy; 04-12-2009 at 12:43 PM.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Quote Originally Posted by Adak View Post
    Code:
     #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 5
    
    
    int largest(int *L){ 
    	int i,big;
    
    	big=L[0];
    	for (i=1;i<SIZE;i++){
    		if (L[i]>big){
    			big=L[i];
    		}
    
    	}	return big;
    }
    
    
    
    int smallest(int *S){
    	int i, small; 
    
    	small=S[0];
    	for (i=1;i<SIZE;i++){
    		if (S[i]<small){
    			small=S[i];
    		}
    
    	}return small;
    }
    
    
    
    void displayArray(int *Array) {
    	int i;
    
    	printf("\nHere is your array:\n\n");
    	for (i=0;i<n;i++) {
                //this is where i get stuck!!
                printf("In element [%i] is the value %i \n",i; Array[i]);
    	}
    	printf("You have %i elements in this array.\n",SIZE);
    }
    
    main (){
    
    	int large,i,small,finish;
    	int element[SIZE];
    
    	do{
    
    		for (i=0;i<SIZE;i++){
    			printf("Enter value for element [%i]: ",i+1);
    			scanf("%i",&element[i]);
    		}
    
    
    		large=largest(element);
    		small=smallest(element);
    		displayArray(element);
    
    
    		printf("\nthe largest value is: %i\n",large);
    		printf("the smallest value is: %i\n\n",small);
    
    		printf("press 1 to quit: ");
    		scanf("%i",&finish);
    
    		system ("pause");
    	}
    
    	while (finish!=1);
    }
    I haven't run it, but that should do it. You weren't passing the array name (which becomes a pointer to the array when it's being passed to a function, thus the *), to DisplayArray. Having no knowledge of what the array was, it printed up garbage.

    If you use prototypes for your functions, it's easier to spot these kinds of problems.
    SCREW MY 2 POSTS AFTER THIS ONE!!! I TRIED IT AND IT WORKED!!! SWEEEEEETTT!!!! CAN YOU TELL ME HOW THAT WORKS??????? THANK YOU THANK YOU THANK YOU!!!!
    (can you tell I'm kinda excited?)

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your array was a local or automatic variable, not a global one. So if you want the other functions to work with the array, you need to tell them where they can find it (an address, which is what a pointer has).

    If Array[size] is your array, then Array (just the name, with no brackets, is the address of the array. That why your big and small function calls have the name "element" in them:

    Code:
    big(element);
    small(element);
    That's on the sending side (it's a send and receive kind of thing with functions). On the receiving side, you need to let the function know what it's got to receive, like so:

    Code:
    int big(int *AnyNameYouWant)  {
    The * tells big it will be getting a pointer, and the int tells it what kind of pointer it will be. The exact name "element" is OK, but we can use an alias for it, just as well. As long as the alias (here, "AnyNameYouWant"), brings the right address, everything works ok.

    Like if you were delivering a no-sign package to 210 Oak St., then you don't care what the guy's name is - you just need to get that address right.

    In C, a whole array is never sent to a function. You send a pointer to the array, instead. Saves a lot of memory and time.
    Last edited by Adak; 04-12-2009 at 02:29 PM.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    thanks adak! that's really clear for me now! though I am having trouble finding the average without having to add each element then / it by SIZE. any suggestions? here's what i got so far.
    Code:
    //this is what i'm doing now
    double theAverage(double M[]){
    double ave;
    ave=(double)(M[0]+M[1]+M[2]+M[3]+M[4])/SIZE;
    
    return ave;
    
    }
    Code:
    //this won't work for some reason
    double theAverage(int *median){
    double ave;
    ave=(double)median[SIZE]/SIZE;
    
    return ave;
    
    }
    
    main(){
    int element;
    average=theAverage(element);
    
    printf("the average is: %lf",average);
    }
    SUGGESTIONS??

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    //this is what i'm doing now
    double theAverage(double M[]){
    double ave;
    ave=(double)(M[0]+M[1]+M[2]+M[3]+M[4])/SIZE;
    
    return ave;
    
    }
    Just use a loop, in your function:

    Code:
    for( i = 0, sum = 0; i < SIZE; i++) 
       sum = sum + M[i];
       //usually coded in C as sum += M[i];
    
    //*now* divide just once
    ave = sum / SIZE;

    Code:
    //this won't work for some reason
    double theAverage(int *median){
    double ave;
    ave=(double)median[SIZE]/SIZE;
    
    return ave;
    
    }
    
    main(){
    int element;
    average=theAverage(element);
    
    printf("the average is: %lf",average);
    }
    SUGGESTIONS??

    Now, it frequently happens that you have an array with say, 100 subscripts:

    Code:
    #define SIZE 100
    int Array[SIZE];
    But you might have only 10 or 50 or 80 numbers that are entered into the Array, and only those numbers have to be averaged or "worked upon" in some way, not 100.

    So what you want to do when there is entry going into the array, is to count up the number of entries, and send that count as an integer, to the various functions that will be working on the data.

    Now when you find a median value, it will be the median of *just* the valid values, in the array.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    This is what i input...and it told me that the subscript is not of integral type
    Code:
    double theAverage(double M[]){
    	double sum,i,ave;
    
    for( i = 0.0, sum = 0.0; i < SIZE; i++) 
       sum = sum + M[i];
      
    ave = sum / SIZE;
    
    
    
    	return ave;
    
    }

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Right.

    "i' needs to be an integer. Sum should be an integer when you are adding integers, but a double when you are adding doubles.

    "ave" definitely should be a double.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  2. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  5. Inputted values into an array
    By Panther in forum C Programming
    Replies: 6
    Last Post: 04-11-2003, 10:15 AM

Tags for this Thread