Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

trying to pass an array using functions?

tonybar2tonybar2 Member Posts: 72
[b][red]This message was edited by the Moderator at 2002-2-13 17:47:40[/red][/b][hr]
ok my program work fine, but I would like to know how to modify my code so that I would pass the array to the functions. without defining variables globaly?
[code]
#include
#define N 7

int maximum(int max);
int average(double avg);
int minimum(int min);

int i, sum = 0, array[N], max, min;
double total = 0.0, avg = 0.0;

int main()
{
for (i = 0; i < N; ++i){
printf("what is temp for day[%d]: ", i + 1);
scanf("%d", &sum);
array[i] = sum;
/* fills the array */
}
system("cls");
printf("

");
for (i = 0; i < N; ++i){
printf("
Temperatures are: %d degrees for day [%d] ", array[i], i+1);
if (array[i] < 32)
printf("(Warning Below Freezing!)");
if (array[i] < 0)
printf("(Negative!)");}
/*prints the array*/
avg = average( avg);
max = maximum( max);
min = minimum( min);
printf("
");
printf("
Avg Temp Max Temp Min Temp
");
printf(" %2.2f %d %d
", avg, max, min);
return 0;
}
average(double avg){
for (i=0;i max)
max = array[i];
}
return max;
}
[/code]
I know that this code has some to be desired but I'm trying, thanks Tony




Comments

  • dmbdmb Member Posts: 18
    ok well what you have to do when passng either an array or a class or any non single type variable is pass it by refference, which means you make a pointer of the same type and make it point to the variable, like this.
    [code]
    int *buff;
    int array[5] = {0,1,2,3,4};
    buff = &array;
    [/code]
    then call the function using the pointer, which points to the array
    [code]
    int callFunct(int &array){
    array[1] = 2;
    }
    ////////
    callFunct(buff);
    }
    [/code]
    that should work, i might be a bit off on teh syntext but the logic is there


    : [b][red]This message was edited by the Moderator at 2002-2-13 17:47:40//*Added Code tags*//[/red][/b][hr]
    : ok my program work fine, but I would like to know how to modify my code so that I would pass the array to the functions. without defining variables globaly?
    : [code]
    : #include
    : #define N 7
    :
    : int maximum(int max);
    : int average(double avg);
    : int minimum(int min);
    :
    : int i, sum = 0, array[N], max, min;
    : double total = 0.0, avg = 0.0;
    :
    : int main()
    : {
    : for (i = 0; i < N; ++i){
    : printf("what is temp for day[%d]: ", i + 1);
    : scanf("%d", &sum);
    : array[i] = sum;
    : /* fills the array */
    : }
    : system("cls");
    : printf("

    ");
    : for (i = 0; i < N; ++i){
    : printf("
    Temperatures are: %d degrees for day [%d] ", array[i], i+1);
    : if (array[i] < 32)
    : printf("(Warning Below Freezing!)");
    : if (array[i] < 0)
    : printf("(Negative!)");}
    : /*prints the array*/
    : avg = average( avg);
    : max = maximum( max);
    : min = minimum( min);
    : printf("
    ");
    : printf("
    Avg Temp Max Temp Min Temp
    ");
    : printf(" %2.2f %d %d
    ", avg, max, min);
    : return 0;
    : }
    : average(double avg){
    : for (i=0;i max)
    : max = array[i];
    : }
    : return max;
    : }
    : [/code]
    : I know that this code has some to be desired but I'm trying, thanks Tony
    :
    :
    :
    :
    :



    [b]---[black]D[gray]M[blue]B
    [/b][italic][size=1][black]All my life I said I wanted to be someone[gray]...[black]I guess I should have been more specific.

Sign In or Register to comment.