C Programming - Collecting data in a function outside of main() - c

Write a program that asks the user to enter daily rainfall amounts. Your program will need to accept 5 daily rainfall inputs. Only allow non-negative rainfall amounts. When the user enters a negative number, tell them that the number is invalid, and that they should enter another, valid value.
Calculate the total rainfall and the average rainfall. Determine the largest daily rainfall and the smallest daily rainfall.
Output the total, average, largest, and smallest values using informative messages.
The following things cannot happen in main:
Accepting user input
Calculation of total or average
Determination of largest or smallest
Outputting results
=============================================
Right now i'm just trying to figure out how to enter the 5 numbers, I have this code so far but it's having me put it in an infinite amount of times. I've been working on this project for hours, so any advice would be amazing.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5 // have the user enter it 5 times
double CollectRainfall() {
double amount;
double rainfall[SIZE];
int i;
printf("Enter a rainfall amount: \n"); // enter amount
scanf_s("%lf", &amount);
for (i = 0; i < SIZE; i++) {
rainfall[i] = CollectRainfall();
while (amount < 0.0) { // if it's a negative number
printf("The number is invalid.\n"); // display error message if a negative # was entered
printf("Enter another rainfall amount: \n");
}
}
}
int main() {
CollectRainfall();
return 0;
}

As said the recursion as you have it will essentially create an infinite loop, and really, for this you dont need it either, you can do something like:
Running sample (commented changes)
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5 // have the user enter it 5 times
void CollectRainfall() { //no return needed
double rainfall[SIZE], sum = 0, max = 0, min = 0;
int i;
for (i = 0; i < SIZE; i++)
{
printf("Enter a rainfall amount: \n"); // enter amount
scanf("%lf", &rainfall[i]); //save values into the array
while (rainfall[i] < 0.0)
{ // if it's a negative number
printf("The number is invalid.\n"); // display error message if a negative # was entered
printf("Enter another rainfall amount: \n");
i--; // iterate back to replace negative number
}
}
printf("Values:");
for (i = 0, min = rainfall[i]; i < SIZE; i++)
{
printf(" %.2lf", rainfall[i]); // print all values
sum += rainfall[i]; // sum values
if(rainfall[i] > max){ //max value
max = rainfall[i];
}
if(min > rainfall[i]){ //min value
min = rainfall[i];
}
}
printf("\nSum: %.2lf", sum); // print sum
printf("\nMax: %.2lf", max); // print max
printf("\nMin: %.2lf", min); // print min
printf("\nAverage: %.2lf", sum / SIZE); //print average
}
int main() {
CollectRainfall();
return 0;
}

You can create a struct to store you data and do the operation.
Something like:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5 // have the user enter it 5 times
typedef struct data {
double rainfall[SIZE];
double average;
double min;
double max;
} data_t;
static void collectRainfall(double rainfall[SIZE]) {
for (int i = 0; i < SIZE; i++) {
double amount;
printf("Enter a rainfall amount: \n"); // enter amount
scanf("%lf", &amount);
while (amount < 0.0) { // if it's a negative number
printf("The number is invalid.\n"); // display error message if a negative # was entered
printf("Enter a rainfall amount: \n"); // enter amount
scanf("%lf", &amount);
}
rainfall[i] = amount;
}
}
static void compute(data_t *data) {
data->min = data->rainfall[0];
data->max = data->rainfall[0];
data->average = data->rainfall[0];
for (int i = 1; i < SIZE; i++) {
double rainfall = data->rainfall[i];
if (rainfall > data->max) {
data->max = rainfall;
}
if (rainfall < data->min) {
data->min = rainfall;
}
data->average += rainfall;
}
data->average /= SIZE;
}
static void display(data_t *data) {
printf("min %f, max %f, average %f\n",
data->min, data->max, data->average);
}
int main() {
data_t data;
collectRainfall(data.rainfall);
compute(&data);
display(&data);
return 0;
}
scanf is a pain in case of bad input best is to read a line then parse it, check if strtod is ok
static void collectRainfall(double rainfall[SIZE]) {
for (int i = 0; i < SIZE; i++) {
char str[32];
double amount = -1;
printf("Enter a rainfall amount [%d/%d]: \n", i , SIZE);
while (42) {
char *res = fgets(str, sizeof(str), stdin);
if (res && (amount = strtod(str, &res)) >= 0 && res != str)
break;
printf("The number is invalid.\n");
printf("Enter a rainfall amount [%d/%d]: \n", i , SIZE);
}
rainfall[i] = amount;
}
}

Related

Get evenly spaced numbers in a range, in C

I am trying to perform an easy calculation without beeing able to figure out the right approach to it: I am trying to obtain a specific amount of N values, reading the step number from user input, within a certain range included between min and max also read from the user input. Those N values must be evenly spaced between themselves.
For instance, this should be able to produce a set of N numbers including the lower range limit and the upper one. I need to use decimal min and max and integer number of step.
This is the code I am trying to use:
#include <stdio.h>
int main()
{
double min;
double max;
int step;
double table_array[step];
table_array[0] = min;
printf("Enter the minimum value: ");
scanf("%lf", &min );
printf("Enter the maximum value: ");
scanf("%lf", &max );
printf("Enter the evenly spaced step value: ");
scanf("%d", &step );
printf("\n----------------------------------\n");
int i;
int increment;
for (i = 0; i <= step; i++){
increment = (max - min) / step;
table_array[i+1] = table_array[i] + increment;
while (table_array[i] < max){
printf("%i %lf\n",i, table_array[i]);
}
}
return 0;
}
I need to improve the for cycle for sure. Any suggestions? I assume it can be even totally wrong since it is not running, or better yet it is running but not showing the right result
First of all, you need declare an array with specific size. Initially the array size is not valid as step will initially have garbage value. then you are trying to print i with %i need to change that one with %d. with your step you need to calculate the increment only once. and as your increment can be double number then just make sure that you are using a double type for increment. now how i calculate the increment as 2 number must be min and max then there must be step >= 2 . suppose max =20 min =10 and step=3 so the numbers will be 10,15,20. so increment = (max -min )/(step-1) = 5. now make some complex case . if step = 1 then min must be equal to max. we need to check this case explicitly. another case if max < min then just need to swap them.
here is the code:
#include <stdio.h>
int
main ()
{
double min;
double max;
int step;
printf ("Enter the minimum value: ");
scanf ("%lf", &min);
printf ("Enter the maximum value: ");
scanf ("%lf", &max);
printf ("Enter the evenly spaced step value: ");
scanf ("%d", &step);
double table_array[step + 5];
table_array[0] = min;
printf ("\n----------------------------------\n");
int i;
double increment;
if (max < min){
double temp;
temp = min;
min = max;
max = temp;
}
if (step == 1 && max == min) {
i = 0;
printf ("%d %.18lf\n", i, table_array[i]);
} else {
increment = (max - min) / (step - 1);
i = 0;
printf ("%d %.18lf\n", i, table_array[i]);
for (i = 1; i < step; i++){
table_array[i] = table_array[i - 1] + increment;
printf ("%d %.18lf\n", i, table_array[i]);
}
}
return 0;
}
#include <stdio.h>
#define SWAPI(min,max,type) do{type tmp; if(min > max) {tmp = max; max = min; min = tmp;}}while(0)
double *getnumbers(double *table, size_t nelements, double max, double min)
{
double step;
if(table && nelements > 1 )
{
SWAPI(min,max, double);
step = (max - min) / (nelements - 1);
for(size_t element = 0; element < nelements; element++, min += step)
{
table[element] = min;
}
}
else
{
table = NULL;
}
return table;
}
void printtable(double *table, size_t num)
{
for(size_t i = 0; i < num; i++)
{
printf("[%zu] = %f\n", i, table[i]);
}
}
int main(int argc, char* argv[])
{
double numbers[25];
if(getnumbers(numbers,2,3,6)) printtable(numbers, 2);
printf("\n");
if(getnumbers(numbers,15,3,6)) printtable(numbers, 15);
}
https://godbolt.org/z/odscs6
#include <stdio.h> // printf, scanf, putchar
#include <float.h> // FLT_EPSILON
#include <math.h> // fabs
int isequal(double, double);
int main(void) {
double min, max, step, nnum;
printf("Maximum: "), scanf("%lf", &max); // Get maximum
printf("Minimum: "), scanf("%lf", &min); // Get minimum
if (min > max) { // If invalid maximum, handle error
puts("Maximum cannot be over minimum");
return 1; // Returning 1 indicates program failure
}
printf("Step: "), scanf("%lf", &step); // Get step
nnum = (max - min)/step;
if (!isequal(nnum, (int) nnum) || step <= 0) { // If invalid step, handle error
puts("Invalid step");
return 1;
}
putchar('\n'); // Separate input from output
for (int i = 0; i <= nnum; i++) {
printf("%d. %lf\n", i + 1, min);
min += step; // We already know it will go into maximum evenly
}
return 0; // Returning 0 indicates program success
}
int isequal(double x, double y) { // fabs() = Float absolute value
return fabs(x - y) < FLT_EPSILON; // FLT_EPSILON = Smallest comparable float value
}

Issue with while loop in C - Rejects input, but then accepts same input second time around

I'm trying to get this program to run properly. It prompts user for a nonzero integer, if a nonzero integer is not entered, the while loop should keep prompting user until a nonzero integer is entered.
Then, it takes the integer and uses that as the amount of double entries the user may enter. The program then calls a function that takes the maximum, minimum, and mean of these user-determined amount of double values.
My issue is that even if I enter a nonzero integer, it rejects it and loops the prompt a second time. But, it accepts the nonzero integer the second time. Code below. Thanks!
#include<stdio.h>
void mmm (int n);
int main()
{
int num;
printf("Enter a positive nonzero integer: ");
scanf("%d", &num);
while(num < 1);
{
printf("Invalid number!\n");
printf("Enter a positive nonzero integer: ");
scanf("%d", &num);
}
mmm(num);
return 0;
}
void mmm (int n)
{
double min, max, mean, sum, entry;
int i;
printf("***** MIN MAX and MEAN *****\n");
printf("Enter %d numbers separated by spaces: ", n);
sum = 0;
for(i = 1; i <= n ; i++)
{
scanf("%lf", &entry);
sum += entry;
if(i == 1)
{
max = entry;
min = entry;
}
if(max < entry)
max = entry;
if(min > entry)
min = entry;
}
mean = sum/n;
printf("Minimum, maximum, and mean of the %d numbers: %.2lf, %.2lf, %.2lf\n", n, min, max, mean);
}
OK I fixed this by implementing a do-while loop in main:
#include<stdio.h>
void mmm (int n);
int main()
{
int num;
do{
printf("Enter a positive nonzero integer: ");
scanf("%d", &num);
}
while (num <= 0);
mmm(num);
return 0;
}

Why is my average not the correct answer? (C code program to find mean)

I am bashing my head because I cannot figure out why my C code keeps printing the wrong average of a set of n numbers!
This is my code below:
int main()
{
int i;
int n;
int sum = 0.0;
int lowest;
int highest;
float average;
int range;
int middle;
double median;
printf("\nEnter the amount of numbers you want?\n");
scanf("%d",&n);
int numbs[n];
int temp[n];
for(i = 0;i < n; i++)
{
printf("\nEnter a number from 0 to 15: ");
scanf("%d",&temp[i]);
}
while (temp[i] < 0 || temp[i] > 15) than 15
{
printf("This number is not from 0 to 15! Please re-enter another number: ");
scanf("%d",&temp[i]);
}
numbs[i] = temp[i];
sum += numbs[i];
}
int sortt = 0, j, x;
for (x = 1; x < n; x++) {
for (j = 0; j < n - x; j++) {
if (numbs[j] > numbs[j + 1]) {
sortt = numbs[j];
numbs[j] = numbs[j + 1];
numbs[j + 1] = sortt;
}
}
}
lowest = numbs[0];
highest = numbs[n-1];
middle = n/2;
if (n % 2)
{
median = numbs[middle];
}
else
{
median = (numbs[middle - 1] + numbs[middle]) / 2.0;
}
average = sum/n;
range = highest - lowest;
printf("\nSum: %d", sum);
printf("\nAverage: %.2f", average);
printf("\nMedian: %.2f", median);
printf("\nRange: %d\n", range);
return 0;
}
This is my input and output below. You can see that 8 divided by 3 is not 2, it is 2.67! I've tried using double and float.
Input & Output:
You need to correct the following line:
average = sum/n;
to
average = (float)sum/n;
You have to cast your return value into float. Think about it as a function with the following definition:
float divide(int x,int y){
return x/y; // returns an integer instead of float.
}
While this definition:
float divide(int x,int y){
return (float)x/y; // creates a temporary float variable and returns it immediately as the returned value of the function.
}
In addition, declaring int sum=0.0 is definitely going to show you a warning when compiling with -Wall. Try to follow warnings that you get from your compiler and fix all of them before you run your program.
8 divided by 3 is 2, remainder 2. 8 and 3 are integers, and when you divide two integers, you use integer division with integer rules.
Also, this line might be confusing you:
int sum = 0.0;
Since sum is an int, this just sets sum to zero.
And:
average = sum/n;
Since both sum and n are integers, this is integer division. What you do with a result does not affect how that result is computed -- C's rules are complex enough already.
/*Here see you can intake all values as float instead */
#include <stdio.h>
#include <stdlib.h>
void main()
{
float i,n,a,b,sum,ave;
printf("This is a program to calculate the average of 'n' numbers \n");
printf("Of How many numbers do you want to calculate average \n");
scanf("%f", &n);
printf("Enter the first number \n");
scanf("%f", &a);
sum = a;
for (i=1;i<n;i++)
{
printf("Enter another number \n");
scanf("%f", &b);
sum = sum + b;
}
ave = (sum/n);
printf("The average of the %f number is %f", n, ave);
getchar();
}

Typing a letter into a number array

I have this simple project where I'm supposed to create 4 arrays and enter employee's data into, the arrays are: The employee's ID, their salary, the salary cuts, and the net salary. I am also supposed to print the average salary, the ID # of the employee with the highest salary, and the ID # of the employee with the lowest cut.
The code works fine, except if I enter a letter, then it just executes the whole program in a weird way (no formatting, random numbers).
Why is that? The letter gets converted to an integer, right? What causes it to stop working properly?
Also, would you mind looking at the coding style I used and telling me if there's a way I can improve it?
#include <stdio.h>
#define SIZE 100
int main(void){
int n; /*Number of employees*/
float average; /*Average of the employee's salary*/
int max; /*Highest salary*/
int min; /*Minimum cut*/
int i;
int employee_number = 1;; /*The number (index +1) of the employee*/
float sum = 0; /*The sum of the employee's salary*/
int num_highest; /*The number of the employee with the highest
salary*/
int num_lowest; /*The number of the employee with the lowest cut*/
int id_number[SIZE]; /*Array to hold employee's IDs in*/
float salary[SIZE]; /*Array for employee's salaries*/
float cuts[SIZE]; /*Array for salary cuts*/
float net_salary[SIZE]; /*Net salary after cuts*/
printf("Enter the number of employees\t");
scanf("%i", &n);
for(i = 0; i < n; i++){
printf("Enter the ID of employee #%i\t", employee_number);
scanf("%i", &id_number[i]);
printf("Enter the salary of employee #%i\t", employee_number);
scanf("%f", &salary[i]);
printf("Enter the salary cut of employee #%i\t", employee_number);
scanf("%f", &cuts[i]);
employee_number++;
}
for(i = 0; i < n; i++){
net_salary[i] = salary[i] - (salary[i] * cuts[i] * 1/100);
}
for(i = 0; i < n; i++){
sum += net_salary[i];
}
average = sum/(float)n;
printf("The average salary is %.2f\n", average);
max = net_salary[0];
min = cuts[0];
for(i = 0; i < n; i++){
if(net_salary[i] > max){
max = net_salary[i];
num_highest = i;
}
if(cuts[i] <= min){
min = cuts[i];
num_lowest = i;
}
}
printf("Employee with ID # %i has the highest salary which is %i\n", id_number[num_highest], max);
printf("Employee with ID # %i has the lowest cut of %i and a base salary of %.2f", id_number[num_lowest], min, salary[num_lowest]);
}
You should check the value returned by every call to function scanf.
If this value is smaller than the number of input arguments that the function is supposed to obtain from the user, then it has failed and you should call it again.
In terms of coding style, you can use a struct to store information about each employee, and create an employees[] array of structs that way. This helps in organizing your code better, and avoids having to use 4 separate arrays to store information about the employees.
The struct can look like this:
typedef struct {
int id_number;
float salary;
float cuts;
float net_salary;
} employee_t;
And then you can create an array of structs like this:
employee_t employees[n]; /* n is number of employees */
It is also good to check the return of scanf().
So instead of doing simply:
scanf("%i", &n);
Be extra careful and change this to:
if (scanf("%i", &n) != 1) {
/*exit program if true */
}
I wrote some code to demonstrate these points:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id_number;
float salary;
float cuts;
float net_salary;
} employee_t;
void get_number_employees(int *n);
void get_user_input(employee_t employees[], int n, float *average);
void print_results(employee_t employees[], int n, float average);
int highest_salary(employee_t employees[], int n);
int lowest_cut(employee_t employees[], int n);
int
main(void) {
int n;
float average;
get_number_employees(&n);
employee_t employees[n];
get_user_input(employees, n, &average);
print_results(employees, n, average);;
return 0;
}
void
get_number_employees(int *n) {
printf("Enter number of employees: ");
if (scanf("%d", n) != 1) {
printf("Invalid number.\n");
exit(EXIT_FAILURE);
}
}
void
print_results(employee_t employees[], int n, float average) {
int high_sal, low_cut;
high_sal = highest_salary(employees, n);
low_cut = lowest_cut(employees, n);
printf("The average salary is %f\n", average);
printf("Employee with ID # %d has the highest salary which is %f\n",
employees[high_sal].id_number, employees[high_sal].net_salary);
printf("Employee with ID # %d has the lowest cut of %f and a base salary of %f\n",
employees[low_cut].id_number, employees[low_cut].cuts, employees[low_cut].salary);
}
void
get_user_input(employee_t employees[], int n, float *average) {
int i, employee_number = 1, sum = 0;
for (i = 0; i < n; i++) {
printf("Enter the ID of employee #%d: ", employee_number);
if (scanf("%d", &(employees[i].id_number)) != 1) {
printf("Invalid number.\n");
exit(EXIT_FAILURE);
}
printf("Enter the salary of employee #%d: ", employee_number);
if (scanf("%f", &(employees[i].salary)) != 1) {
printf("Invalid salary.\n");
exit(EXIT_FAILURE);
}
printf("Enter the salary cut of employee #%d: ", employee_number);
if (scanf("%f", &(employees[i].cuts)) != 1) {
printf("Invalid cuts.\n");
exit(EXIT_FAILURE);
}
employees[i].net_salary = employees[i].salary - (employees[i].salary * employees[i].cuts * 1/100);
sum += employees[i].net_salary;
employee_number++;
}
*average = (1.0f * sum)/n;
}
int
highest_salary(employee_t employees[], int n) {
float max;
int i, idx;
max = employees[0].net_salary;
for (i = 1; i < n; i++) {
if (employees[i].net_salary > max) {
max = employees[i].net_salary;
idx = i;
}
}
return idx;
}
int
lowest_cut(employee_t employees[], int n) {
float min;
int i, idx;
min = employees[0].cuts;
for (i = 1; i < n; i++) {
if (employees[i].cuts < min) {
min = employees[i].cuts;
idx = i;
}
}
return idx;
}
Letters are char or char* when taken as input for many C and C++ functions. char can generally be output to the console as an ASCII encoded digit or as an integer value.
char can be cast into int very easily in C because it's actually considered a numeric value. For example, A becomes 41, and B becomes 42.

For/while loop doesn't update value properly

I'm trying to write a program which finds the weighted average (for grades and such).
Through user input, I create two arrays of the same size, one carrying the weights for each grading category, and the other holding the users score in the corressponding category. For example, if a student has a score of 92% in category 1, which is worth 30% of the total grade, then weights[1] = .3 and scores[1] = .92. This way, the average in each category can be found through (weights[i] * scores[i]) where i is an instance variable initialized in a for loop to go through each array entirely. The problem here is in finding the cumulative average- my program is written to compound the values of each average it handles for example I have tried code like average = average + (weights[i] * scores[i]) to obtain the final cumulative average. However through print statements I've found the value of average is set to the last (weights[i] * scores[i]) operation performed, ignoring the previous value of average (or taking it to be 0). For example, if weights[]={.3, .3, .4} and scores[] = {.4, .4 , .4}. The cumulative average should be .4, but my program tells me .32, which is the last value of weights times the last value of scores.
methods.c:
#include <stdio.h>
#include "methods.h"
int howManyCategories()
{
int categories = 0;
int firstTime = 0;
while(1)
{
if(firstTime == 0)
{
fprintf(stdout, "\nHow many categories are you currently graded on? ");
scanf("%d", &categories);
}
else
{
fprintf(stdout, "\nThat's not a valid value, try a positive integer. ");
scanf("%d", &categories);
}
firstTime++;
if(categories > 0)
{
return categories;
}
}
}
double howMuchWeight(int categories)
{
double weight = 0;
while(1)
{
if(categories == 1)
{
fprintf(stdout,
"\nThis %d category accounts for how much of your grade? (.1, .85, 1 etc.) ",
categories);
scanf("%lf", &weight);
}
else
{
fprintf(stdout,
"\nThese %d categories account for how much of your grade? (.1, .85, 1 etc.) ",
categories);
scanf("%lf", &weight);
}
if(weight > 1 || weight <= 0)
{
fprintf(stdout,
"\nRemember, total weighting must be positive and no larger than 1"
" (100 percent). \nTry Again.\n");
}
else
{
return weight;
}
}
}
double findWeightsAndScores(int i, double categories,
double checkWeights, double totalWeight,
double scores[])
{
while(1)
{
double piece = 0;
int count = i + 1;
fprintf(stdout, "\nWeight for category %d: ", count);
scanf("%lf",&piece);
if(piece > 0 && piece < 1)
{
findScores(i, &scores[i]);
return piece;
}
else
{
fprintf(stdout, "\nRemember, weights must be positive and less than 1!\n");
}
}
}
void findScores(int i, double scores[])
{
int validScore = 0;
while(validScore == 0)
{
int count = i + 1;
double score = 0;
fprintf(stdout, "Please enter your percentage score in category %d: ", count);
scanf("%lf", &score);
if(score >= 0 && score <= 1)
{
scores[i] = score;
validScore = 1;
}
else
{
fprintf(stdout, "\nRemember, scores in each category must be positive, no greater "
"than 1 (100 percent) and no less than 0, try again.\n\n");
}
}
}
double findAverage(int categories, double weights[], double scores[])
{
double average = 0;
for(int i = 0; i <= categories - 1; i++)
{
average += (weights[i] * scores[i]);
fprintf(stdout, "%lf", average);
}
return average;
}
methods.h:
#ifndef METHODS_H_
#define METHODS_H_
int howManyCategories();
double howMuchWeight(int categories);
double findWeightsAndScores(int i, double categories,
double checkWeights, double totalWeight, double scores[]);
void findScores(int i, double scores[]);
double findAverage(int categories, double weights[], double scores[]);
#endif
whatsmyGrade.c:
#include <stdio.h>
#include "methods.h"
int main()
{
while(1)
{
/*Prompts for categories*/
int categories = howManyCategories();
int dataProvided = 1;
double checkWeights = 0;
double grade = 0;
double average = 0;
/*Prompts for total weight*/
double totalWeight = howMuchWeight(categories);
double category[categories];
double weights[categories];
double scores[categories];
/*Assign weights to each category and score in each category*/
for(int i = 0; i <= categories - 1; i++)
{
/*Fill array for weights*/
weights[i] = findWeightsAndScores(i, categories, checkWeights, totalWeight,
scores);
checkWeights = checkWeights + weights[i];
if(checkWeights != totalWeight && i == categories - 1)
{
fprintf(stdout, "\nYour weight distribution is invalid! Try again.\n");
dataProvided = 0;
break;
}
}
/*Calculate total grade*/
if(dataProvided == 1)
{
average = findAverage(categories, weights, scores);
grade = (average / totalWeight) * 100;
fprintf(stdout, "Your cumulative average is %.2lf percent\n\n", grade);
return 1;
}
}/*End of parent while loop*/
}/*End of main*/
Thank you in advance
Your problem is somewhere else because it works like a charm for me:
#include <stdio.h>
double findAverage(int categories, double weights[], double scores[])
{
double average = 0;
int i;
for(i = 0; i <= categories - 1; i++) {
average += (weights[i] * scores[i]);
fprintf(stdout, "%lf ", average);
}
return average;
}
int main(void) {
// your code goes here
double average;
double grade;
int categories = 3;
double totalWeight = .3 + .3 + .4;
double weights[] = {.3, .3, .4};
double scores[] = {.4, .4, .4};
average = findAverage(categories, weights, scores);
grade = (average / totalWeight) * 100;
fprintf(stdout, "\nYour cumulative average is %.2lf percent\n\n", grade);
return 0;
}
Got it, when I call the findScores function in the findWeightsAndScores function I was passing it &scores[i] when I should have been passing it scores. Thanks guys

Resources