![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 9
| 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);
}
|
| hiddenprophecy is offline | |
| | #2 |
| Making mistakes Join Date: Dec 2008
Posts: 347
| printf("The element [%i] of the array is %i\n", i + 1, elements[i]); |
| Brafil is offline | |
| | #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 |
| hiddenprophecy is offline | |
| | #4 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| you need to pass your array as a parameter to the displayArray function
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #5 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| 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);
}
If you use prototypes for your functions, it's easier to spot these kinds of problems. |
| Adak is offline | |
| | #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;
|
| hiddenprophecy is offline | |
| | #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. |
| hiddenprophecy is offline | |
| | #8 | |
| Registered User Join Date: Apr 2009
Posts: 9
| Quote:
(can you tell I'm kinda excited?) | |
| hiddenprophecy is offline | |
| | #9 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| 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); Code: int big(int *AnyNameYouWant) {
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. |
| Adak is offline | |
| | #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);
}
|
| hiddenprophecy is offline | |
| | #11 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| 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: 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);
}
Now, it frequently happens that you have an array with say, 100 subscripts: Code: #define SIZE 100 int Array[SIZE]; 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. |
| Adak is offline | |
| | #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;
}
|
| hiddenprophecy is offline | |
| | #13 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| 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. |
| Adak is offline | |
![]() |
| Tags |
| arrays, display function, displaying array values, functions, void display array |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Creating a menu that reads input via an array? | Nalif | C Programming | 6 | 09-29-2006 09:21 PM |
| User input for array size. | coolmoniker | C++ Programming | 27 | 08-24-2006 11:34 PM |
| Quick question about SIGSEGV | Cikotic | C Programming | 30 | 07-01-2004 07:48 PM |
| Very odd segmentation fault/core dump. | ChristianTool | C Programming | 19 | 04-26-2004 06:38 AM |
| Inputted values into an array | Panther | C Programming | 6 | 04-11-2003 10:15 AM |