Starbuck<3

12-14-2011, 01:36 AM

I am very new to programming.

I have been looking to many examples for help with a program I need to write, but I am not sure I am getting it. :confused:

What I have to do:

Write a C program to callculate an average of an array of numbers and produce the following output:

1. All the numbers in the array

2. The sum of all the numbers in the array

3. The count of all the numbers in the array

4. The average of all the numbers in the array

Double-space after lines 1, 2, and 3

Define a 1 dimensional integer array of 8 elements and use scanf to read in 8 integers of your choice.

Use a double alternative IF block to calculate avg. of the array and to check for division by 0.

Use for loops to process the array elements and to output numbers

What I have so far:

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])

{

/* Declaring */

int count;

int values[8];

int ave, i;

int sum = 0;

/* Prompting for values to fill array 'values' */

printf("Enter 8 values: \n");

for (i = 0; i < 8; i++)

scanf("%d", &values[i]);

/* Outputting values */

printf("Your numbers are: \n");

for(i = 0; i < 8; i++)

printf("%4d",values[i]);

/* Calculating average */

for(i = 0; i < 8; i++)

if(count > 0)

ave = sum / count;

else

{

ave = 0;

printf("Invalid amount of values")

}

printf("\nThe average of the ten numbers are: %d\n", ave);

system("pause");

return 0;

}

I have been looking to many examples for help with a program I need to write, but I am not sure I am getting it. :confused:

What I have to do:

Write a C program to callculate an average of an array of numbers and produce the following output:

1. All the numbers in the array

2. The sum of all the numbers in the array

3. The count of all the numbers in the array

4. The average of all the numbers in the array

Double-space after lines 1, 2, and 3

Define a 1 dimensional integer array of 8 elements and use scanf to read in 8 integers of your choice.

Use a double alternative IF block to calculate avg. of the array and to check for division by 0.

Use for loops to process the array elements and to output numbers

What I have so far:

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])

{

/* Declaring */

int count;

int values[8];

int ave, i;

int sum = 0;

/* Prompting for values to fill array 'values' */

printf("Enter 8 values: \n");

for (i = 0; i < 8; i++)

scanf("%d", &values[i]);

/* Outputting values */

printf("Your numbers are: \n");

for(i = 0; i < 8; i++)

printf("%4d",values[i]);

/* Calculating average */

for(i = 0; i < 8; i++)

if(count > 0)

ave = sum / count;

else

{

ave = 0;

printf("Invalid amount of values")

}

printf("\nThe average of the ten numbers are: %d\n", ave);

system("pause");

return 0;

}