C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-12-2009, 11:15 AM   #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);
}
hiddenprophecy is offline   Reply With Quote
Old 04-12-2009, 11:18 AM   #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   Reply With Quote
Old 04-12-2009, 11:29 AM   #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   Reply With Quote
Old 04-12-2009, 12:20 PM   #4
CSharpener
 
vart's Avatar
 
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   Reply With Quote
Old 04-12-2009, 12:24 PM   #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);
}
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.
Adak is offline   Reply With Quote
Old 04-12-2009, 12:31 PM   #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
hiddenprophecy is offline   Reply With Quote
Old 04-12-2009, 12:41 PM   #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   Reply With Quote
Old 04-12-2009, 12:48 PM   #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?)
hiddenprophecy is offline   Reply With Quote
Old 04-12-2009, 02:26 PM   #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);
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.
Adak is offline   Reply With Quote
Old 04-12-2009, 03:15 PM   #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??
hiddenprophecy is offline   Reply With Quote
Old 04-12-2009, 04:53 PM   #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;

}
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.
Adak is offline   Reply With Quote
Old 04-12-2009, 05:14 PM   #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   Reply With Quote
Old 04-12-2009, 05:49 PM   #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   Reply With Quote
Reply

Tags
arrays, display function, displaying array values, functions, void display array

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:59 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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