• 04-12-2009
hiddenprophecy
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...

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); }```
• 04-12-2009
Brafil
printf("The element [%i] of the array is %i\n", i + 1, elements[i]);
• 04-12-2009
hiddenprophecy
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
• 04-12-2009
vart
you need to pass your array as a parameter to the displayArray function
• 04-12-2009
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.
• 04-12-2009
hiddenprophecy
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
• 04-12-2009
hiddenprophecy
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
• 04-12-2009
hiddenprophecy
Quote:

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?)
• 04-12-2009
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.
• 04-12-2009
hiddenprophecy
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??
• 04-12-2009
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.
• 04-12-2009
hiddenprophecy
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; }```
• 04-12-2009