# Function Help

Page 1 of 1

## 5 Replies - 574 Views - Last Post: 21 April 2013 - 04:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319064&amp;s=ce897472f32d4b991c2be8ff8c53c4fe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 joelriter

• New D.I.C Head

Reputation: 0
• 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

Reputation: 4895
• Posts: 15,288
• 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

### #3 joelriter

• New D.I.C Head

Reputation: 0
• 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).

### #4 joelriter

• New D.I.C Head

Reputation: 0
• 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");
}

```

### #5 jimblumberg

Reputation: 4895
• Posts: 15,288
• 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

### #6 joelriter

• New D.I.C Head

Reputation: 0
• 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.