5 Replies - 1815 Views - Last Post: 20 October 2009 - 05:05 PM Rate Topic: -----

#1 izthrower   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

pass by value pass by reference in c

Posted 20 October 2009 - 04:13 PM

a. Modify the main function to print elements of the fArray to the screen using the printFloatArray function.

b. Complete the sumAvgArray function to calculate the summation and average of all the elements in a given float array. The function will use the arguments sum and avg to return the values calculated to the calling function.

c. Modify the main function to call sumAvgArray function to calculate sum and average of all the elements in the fArray.
/*
 * PURPOSE OF THIS PROGRAM:
 *
 */

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5

/*
 * ************************************
 * FUNCTION DECLARATIONS
 * ************************************
 */
void printFloatArray(float array[], int size);
void sumAvgArray(float array[], int size, float *sum, float *avg);


int main(){
/***********************************
 * Declarations for main function
 ***********************************/
		float fArray[SIZE]={5.2, -5.2, 4.3, 4.0, 0.0};
		float summation, average;

/**********************************
 * Statements for main function
 **********************************/

		printf("\n\nThe array has %d elements. The element of the array is\n", SIZE);
		printFloatArray(fArray,SIZE);
		//
		// write a function call to print each element of the array fArray using printFloatArray function
		//
		//

		//
		// write a function call to calculate
		// sum and average of the elements in the fArray using sumAvgArray function
		//


		printf("\nsum=  %.2f \naverage= %.2f \n", summation, average);

		return 0;
}

/*
 * ************************************
 * FUNCTION DEFINITIONS
 * ************************************
 */
//

// PURPOSE: a function to print elements of a float array
//
// INPUTS:
//	  - array: is the array to print
//	  - size: size of the array
// RETURNS: none
//
void printFloatArray(float array[], int size)
{
		int i;

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

//
// PURPOSE:
//
// INPUTS:
//
// RETURNS:
void sumAvgArray(float array[], int size, float *sum, float *avg)
{
int i, sum = 0;
for(i = 0;i < size;i++)
{
sum = sum + array[i];
}
return(sum);
}


}



how do i do the average?
also i have a problem when i compile it it gives me no errors but when i run it it takes away my prompt on my turminal and i cant get it back.
Can some one help?

Is This A Good Question/Topic? 0
  • +

Replies To: pass by value pass by reference in c

#2 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: pass by value pass by reference in c

Posted 20 October 2009 - 04:15 PM

for the avg

int i, sum = 0;
for(i = 0;i < size;i++)
{
sum = sum + array[i];
}
return(sum/size);


This post has been edited by ImaSexy: 20 October 2009 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 izthrower   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: pass by value pass by reference in c

Posted 20 October 2009 - 04:20 PM

View PostImaSexy, on 20 Oct, 2009 - 03:15 PM, said:

for the avg

int i, sum = 0;
for(i = 0;i < size;i++)
{
sum = sum + array[i];
}
return(sum/size);



you cant return anything its a void function

so would i do

average = sum/size;

do you know why when i run the program it takes my promt away?
Was This Post Helpful? 0
  • +
  • -

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: pass by value pass by reference in c

Posted 20 October 2009 - 04:25 PM

Take a look at the comments, im a c++ guy but it didnt matter in this case
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5


void printFloatArray(float array[], int size);
//You wanna pass variable by reference right, you need to have them a parameters
void sumAvgArray(float array[], float size, float &avg, float &summation );


int main(){

		float fArray[SIZE]={5.2, -5.2, 4.3, 4.0, 0.0};
		float summation, average;



		printf("\n\nThe array has %d elements. The element of the array is\n", SIZE);
		printFloatArray(fArray,SIZE);
 


		//call the sum function, which also averages now
		sumAvgArray(fArray,5.0,average,summation);


		printf("\nsum=  %.2f \naverage= %.2f \n", summation, average);


		system("pause");
		return 0;
}

//This functions great
//Be careful with calling something array, i think its a keyword
void printFloatArray(float array[], int size)
{
		int i;

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

// Take a look, size should be float if you want to be accurate
void sumAvgArray(float array[], float size, float &avg, float &summation)
{
	int i, sum = 0;
	for(i = 0;i < size;i++)
		sum = sum + array[i];
	//average is just the total divided by the amount of items
	//these variables were passed by reference back to main
	avg = sum/size;
	summation = sum;
}


Was This Post Helpful? 0
  • +
  • -

#5 izthrower   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: pass by value pass by reference in c

Posted 20 October 2009 - 04:43 PM

View PostImaSexy, on 20 Oct, 2009 - 03:25 PM, said:

Take a look at the comments, im a c++ guy but it didnt matter in this case
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5


void printFloatArray(float array[], int size);
//You wanna pass variable by reference right, you need to have them a parameters
void sumAvgArray(float array[], float size, float &avg, float &summation );


int main(){

		float fArray[SIZE]={5.2, -5.2, 4.3, 4.0, 0.0};
		float summation, average;



		printf("\n\nThe array has %d elements. The element of the array is\n", SIZE);
		printFloatArray(fArray,SIZE);
 


		//call the sum function, which also averages now
		sumAvgArray(fArray,5.0,average,summation);


		printf("\nsum=  %.2f \naverage= %.2f \n", summation, average);


		system("pause");
		return 0;
}

//This functions great
//Be careful with calling something array, i think its a keyword
void printFloatArray(float array[], int size)
{
		int i;

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

// Take a look, size should be float if you want to be accurate
void sumAvgArray(float array[], float size, float &avg, float &summation)
{
	int i, sum = 0;
	for(i = 0;i < size;i++)
		sum = sum + array[i];
	//average is just the total divided by the amount of items
	//these variables were passed by reference back to main
	avg = sum/size;
	summation = sum;
}



ok when i do that it gives me errors these are the errors i dont get them
sum.c:41: error: incompatible type for argument 3 of ‘sumAvgArray’
sum.c:41: error: incompatible type for argument 4 of ‘sumAvgArray’
sum.c: In function ‘sumAvgArray’:
sum.c:85: error: invalid operands to binary +
sum.c:87: error: invalid operands to binary /
Was This Post Helpful? 0
  • +
  • -

#6 izthrower   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: pass by value pass by reference in c

Posted 20 October 2009 - 05:05 PM

ok so i figured out how to remove the errors
this is my code
 /*
 * PURPOSE OF THIS PROGRAM:
 *
 */

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5

/*
 * ************************************
 * FUNCTION DECLARATIONS
 * ************************************
 */
void printFloatArray(float array[], int size);
void sumAvgArray(float array[], int size, float *sum, float *avg);


int main(){
/***********************************
 * Declarations for main function
 ***********************************/
		float fArray[SIZE]={5.2, -5.2, 4.3, 4.0, 0.0};
		float summation, average;

/**********************************
 * Statements for main function
 **********************************/
		printf("\n\nThe array has %d elements. The element of the array is\n", SIZE);
		printFloatArray;
		//
		// write a function call to print each element of the array fArray using printFloatArray function
		//
		//

		//
		// write a function call to calculate
		// sum and average of the elements in the fArray using sumAvgArray function
		//

		sumAvgArray;
		printf("\nsum=  %.2f \naverage= %.2f \n", summation, average);

		return 0;
}

/*
 * ************************************
 * FUNCTION DEFINITIONS
 * ************************************
 */

//

// PURPOSE: a function to print elements of a float array
//
// INPUTS:
//	  - array: is the array to print
//	  - size: size of the array
// RETURNS: none
//
void printFloatArray(float array[], int size)
{
		int i;

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

//
// PURPOSE:
//
// INPUTS:
//
// RETURNS:
//
void sumAvgArray(float array[], int size, float *sum, float *avg)
{
		int i;
		for(i = 0;i < size;i++)
		{
				*sum = *sum + array[i];
		}
		*avg = *sum / size;
}



i still have a problem when i run it it doesnt do anything
it doesnt even print out the couple of lines its suposed to
when i run it it takes away my promt and doesnt do anything
can some one help plz?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1