5 Replies - 374 Views - Last Post: 21 April 2013 - 04:40 PM Rate Topic: -----

#1 joelriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-April 13

Function Help

Posted 20 April 2013 - 08:05 AM

Hello, I know this isn't the place to introduce myself, but I am new here and this is my first post, I've been lurking for a while so I hope that how I approach this is in the correct way.

I'm currently in an introduction to programming course and my current assignment is using functions. I thought I had a good grasp, but alas I am for once in this course completely stuck. I do not have the time to ask my professor for help (the assignment is due tomorrow) so any advice or suggestions would be completely appreciated.

This is the code I have so far :

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

double getValidRainfall(){
	double result;

	printf("\n Enter a rainfall amount:  ");
	scanf("%lf", &result);

	while (result < 0.0) {
			printf("%.2lf is not a valid rainfall amount.", result);
			printf("\n Enter another rainfall amount:  ");
	        scanf("%lf", &result);
	}

	return result;
}

double calculateTotal(double rainfall[], int size){
	double result = 0.0;
	int i;
	for(i = 0; i < size; i++){
		result += rainfall [i];
	}
	return result;
}

double findLargest(double rainfall[], int size){
	double result = 0.0;
	int i;
	for(i = 0; i < size; i++){
		if(rainfall[i] > result)
			result = rainfall[i];
	}
	return result;
}

double findSmallest(double rainfall [], int size){
	double result = 0.0;
	int i;
	for(i = 0; i < size; i++){
		if(rainfall[i] < result)
			result = rainfall[i];
	}
	return result;
}

double calculateResults(){
	double rainfall[SIZE], totalRainfall, averageRainfall, smallestRainfall;
	double largestRainfall, result = 0.0;

	totalRainfall = calculateTotal( rainfall, SIZE );
	averageRainfall = totalRainfall / SIZE;
	largestRainfall = findLargest(rainfall, SIZE);
	smallestRainfall = findSmallest(rainfall, SIZE);

	return result;
}

double displayOutput(){
	double rainfall[SIZE], totalRainfall, averageRainfall, smallestRainfall;
	double largestRainfall, result;
	calculateResults();

	printf("The total rainfall is %.2lf\n", totalRainfall);
	printf("The average daily rainfall is %.2lf\n", averageRainfall);
	printf("The largest daily rainfall is %.1lf\n", largestRainfall);
	printf("The smallest daily rainfall is %.1lf\n", smallestRainfall);

	return result;
}

main(){
	double rainfall[SIZE], totalRainfall, averageRainfall, smallestRainfall;
	double largestRainfall;
	int i;

	for( i = 0; i < SIZE; i++)
		rainfall[i] = getValidRainfall();


	
	displayOutput();

	system("pause");
}



No matter what I try to do the variables won't initialize correctly. I need a function to display the output. Could someone explain to me how to go about this or give me advice? I recognize there are probably far better ways to write this program, but I am stuck with the assignment being what it is. Thank you in advance.

This post has been edited by jimblumberg: 20 April 2013 - 08:08 AM
Reason for edit:: Fixed Code Tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Function Help

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • Joined: 25-December 09

Re: Function Help

Posted 20 April 2013 - 08:12 AM

If you want to print your array in your function you must pass that array to the function not create a new array.


You may want to study the function tutorials in my signature.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 joelriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-April 13

Re: Function Help

Posted 20 April 2013 - 08:16 AM

Ok thank you! I did not even realize I was creating a new array hahaha! I'll definitely look over your tutorial. Thank you for pointing me in the right direction. I think it's funny how the boards frequently remind users that homework will not be done for them, I understand it gets difficult but I know I enjoy coding and I want to learn and solve the problems for myself (not that I don't need help sometimes hahahaha).
Was This Post Helpful? 0
  • +
  • -

#4 joelriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-April 13

Re: Function Help

Posted 21 April 2013 - 04:17 PM

This is driving me insane. I thought I got this code ot work but I either get too few argument too call or a missing ";" before type error. All I'm trying to do is setup a function to run several printf statements with previously initialized variables. I have tried many different solutions but either I end up with the variables being uninitialized or something else. Please help me! I have read through many function guides and I do not know what I'm missing!

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

double getValidRainfall(){
	double result;

	printf("\n Enter a rainfall amount:  ");
	scanf("%lf", &result);

	while (result < 0.0) {
			printf("%.2lf is not a valid rainfall amount.", result);
			printf("\n Enter another rainfall amount:  ");
	        scanf("%lf", &result);
	}

	return result;
}

double calculateTotal(double rainfall[], int size){
	double result = 0.0;
	int i;
	for(i = 0; i < size; i++){
		result += rainfall [i];
	}
	return result;
}

double findLargest(double rainfall[], int size){
	double result = 0.0;
	int i;
	for(i = 0; i < size; i++){
		if(rainfall[i] > result)
			result = rainfall[i];
	}
	return result;
}

double findSmallest(double rainfall [], int size){
	double result = 0.0;
	int i;
	for(i = 0; i < size; i++){
		if(rainfall[i] < result)
			result = rainfall[i];
	}
}

void displayResult(double totalRainfall, double averageRainfall, double smallestRainfall, double largestRainfall){


	printf("The total rainfall is %.2lf\n", totalRainfall);
	printf("The average daily rainfall is %.2lf\n", averageRainfall);
	printf("The largest daily rainfall is %.1lf\n", largestRainfall);
	printf("The smallest daily rainfall is %.1lf\n", smallestRainfall);


}

main(){
	double rainfall[SIZE], totalRainfall, averageRainfall, smallestRainfall;
	double largestRainfall;
	int i;

	for( i = 0; i < SIZE; i++)
		rainfall[i] = getValidRainfall();

	totalRainfall = calculateTotal( rainfall, SIZE );
	averageRainfall = totalRainfall / SIZE;
	largestRainfall = findLargest(rainfall, SIZE);
	smallestRainfall = findSmallest(rainfall, SIZE);
	
	void displayResult();

	system("pause");
}


Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • Joined: 25-December 09

Re: Function Help

Posted 21 April 2013 - 04:29 PM

Do you see any difference between the following two function calls?

	smallestRainfall = findSmallest(rainfall, SIZE);

	void displayResult();



When working with functions your function implementation, definition, and function call must all agree as to the type and number o parameters. Also void displayResult(); is a function definition, not a function call.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 joelriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-April 13

Re: Function Help

Posted 21 April 2013 - 04:40 PM

I figured it out, I wasn't inputting the variable names in the function call for the printf statementfunction. Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1