Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,182 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,415 people online right now. Registration is fast and FREE... Join Now!




returning an integer from a function

 
Reply to this topicStart new topic

returning an integer from a function

ability
post 7 Oct, 2008 - 04:55 PM
Post #1


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 22



Thanked 1 times
My Contributions


Hello all,

I am working on a project that will ask for data from a user and the program will eventually be menu driven, but currently is not. The data will give the number of items that are in a array, what the data is, the highest and the lowest numbers are and what their average are.

The MAX size of the array will be changed from the current 10 to 200. I need to be able to return what the actual number of cells of the array re being used so the program does not have to run through all 200. I added a printf statement in my main to confirm if I returned the value or not and it does not. In the function enter(), I have a printf to see if it will show the number found in arraySize. I typed in return arraySize, but this does not do it?

Suggestions?

CODE


#include <stdio.h>
#define MAX 10

int enter(int array[], int count);
void print(int array[], int size);
int counter(int array[], int size);
void large ( int array[], int size);
void small ( int array[], int size);


void average(int array[], int size);

int main()
{
    int myarray[MAX];
    int count;
    int arraySize = 0;


printf("Enter one data item after each prompt\n");
printf("Press return after each one.\n");
printf("Signal with -1 when you are done with data input\n");
enter(myarray, MAX);
printf("\n");

printf( "Count again is %d\n:" , arraySize);
printf("\n");


printf("The numbers that are in the array are: ");

print(myarray, arraySize);
printf("\n");

counter(myarray, arraySize);
printf("\n");


average( myarray, arraySize);
printf("\n");

large ( myarray, arraySize);
printf("\n");

small ( myarray, arraySize);
printf("\n");

return 0;
}






/*******************functions*********************/

int enter(int array[], int count)
{
int i = 0;
int arraySize = 0;


while ( i < count  && array[i] != -1)
{
   i++;
   arraySize++;
   scanf("%d", &array[i] );
}

printf("Array size is :%d\n", arraySize);


return arraySize;

}


void print(int array[], int size)
{
int i;

for ( i = 0; i < size; i++)
      printf("%d  ", array[i] );
      printf("\n");
}


                 /**************/

int counter(int array[], int size)
{
int i;

    for (i=0; i < size; i++)
     size++;
printf("The number of items that are in the array are/is %d\n", i);

}


void large ( int array[], int size)
{
int i;
int largest = array[0];
for ( i = 1; i < size; i++ )
{
    if (array[i] > largest )
        largest = array[i];
}
printf ( "Largest %d\n", largest );
}


void small ( int array[], int size)
{
int i;
int smallest = array[0];
for ( i = 1; i < size; i++ )
{
    if (array[i] < smallest )
        smallest = array[i];
}
printf ( "Smallest %d\n", smallest );
}


void average(int array[], int size)
{
int i;
int sum = 0;
int average = 0;

for ( i = 0; i < size; i++)
     sum = sum + array[i];

     average = sum / size;

      printf("Average %d\n ", average);
    
}





Of course your help is greatly appreciated.

JC
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 7 Oct, 2008 - 05:00 PM
Post #2


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


You have to assign the return value from a function to one of your variables.
All variables are local to whatever function contains them, meaning if you have two functions and they both contain an int called i, the two i's are not related. setting one of them to 5 won't change the value of the other i.
User is online!Profile CardPM

Go to the top of the page

ability
post 7 Oct, 2008 - 05:59 PM
Post #3


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 22



Thanked 1 times
My Contributions


QUOTE(GWatt @ 7 Oct, 2008 - 06:00 PM) *

You have to assign the return value from a function to one of your variables.
All variables are local to whatever function contains them, meaning if you have two functions and they both contain an int called i, the two i's are not related. setting one of them to 5 won't change the value of the other i.




That seem to make since, However, I cannot visualize the code. I have tried a few things, but my results do not change.
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 7 Oct, 2008 - 06:10 PM
Post #4


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


val = function(...);

val is of any type you want, and function is a function that returns the same type as whatever val is.

i guess in your code it would be:
arraySize = counter(array, arraySize);
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 03:54PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month