954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Average of an array

I'm trying to write a program that will accept input from the user as sales, then it will total the sales and display both the total and the sales tax (total multiplied by .0825

#include <stdio.h>
#include <stdlib.h>
#define SIZE 9
int main(void)
{
    /*initialize */
 int a[SIZE]= {0};
 int  total, sales, tax;
/*input data */
 printf ( "Enter sales (enter -1 to end):$ ");
        scanf ("%d", &sales );
 while ( sales != -1 ){
       total = sum_array(a,SIZE) ;
       tax = total * .0825;
 printf ("Enter sales in a week(enter -1 to end):$ ");
        scanf ( "%d", &sales );
 }
 printf("total =  %d\n", sales);
 printf("sales tax= %d\n", total);
  system("PAUSE");	
  
}
int sum_array(int a[], int num_elements)
{
   int i, sum=0;
   for (i=0; i<num_elements; i++)
   {
	 sum = sum + a[i];
   }
   return(sum);
}


output

Enter sales (enter -1 to end):$ 100
Enter sales in a week(enter -1 to end):$ 200
Enter sales in a week(enter -1 to end):$ 200
Enter sales in a week(enter -1 to end):$ -1
total = -1
sales tax= 0
Press any key to continue . . .

jvill
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Jeez, are you posting every question in your assignment?

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

actualy its just part of a like 6 part program and these two parts im having trouble with, im combining them all into one, the other 4 i have done and working

jvill
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

and obviously i've put work into it, its not like im one of those people just asking for a free answer

jvill
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Your use of int is going to be a problem first. Rounding comes into play here. You should do your multiplications in double or float.

It appears you may also want to store each of the sales input into the array a. Hence you should first get all of the sales, assign each input to a[i]. You also need to make sure that no more that 9 inputs are entered, so you should keep a count of each input and terminate input when either salses = -1 of input_count >= 9. Reason is if more than 9 is entered, then you will overflow the array a and cause a bad memory access. ie: program crashesm if user enters 10 or 11 inputs.

erroldo
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

other issues also with summing up the sales.
Have a loop that first gets all inputs:

float a[SIZE];
int sales_count = 0;
while(sales != -1 && sales_count < 9) {
printf("\tInput sale item %d", sales_count);
scanf("%f", &sales);
a[sales_count] = sales;
}

// now you can call the average function and average array a, etc.

erroldo
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: