Arrays

Hi everyone i hope that you are doing well
i have a question please about the arrays, i gave each case in the array a price for a specific item, later on i suggest in the script to input the quantity of each item, what i want to do is sum up the total of all (price*quantity) if someone can help me.
Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include<stdio.h>
#include<math.h>
#define MAX_SIZE 1000
int main()
{
	float arr[MAX_SIZE];
	float quantity;
	int k,i,N;
	float total;
	printf("Please enter the number of the items ");
	scanf("%d",&N);
	printf("Please enter the price of each item \n");
	for(i=0; i<N; i++){
		scanf("%f",&arr[i]);
}
k=0;
    for(i=0; i<N; i++){
    	k=k+1;
    	printf("Please enter the quantity of item %d \n ",k);
    	scanf("%f",&quantity);
    	total=quantity*arr[i];
	}
	printf("the total amount is %f",total);
	return 0;
}
Hello Snow14,

It is not good practice to start a new topic on the same subject. It confuses people not knowing which topic to respond to. And deleting your original message leaves answers to an unknown question.

What I was about to show you was this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<math.h>  // <--- I do not believe there is any use for this header file.

#define MAX_SIZE 1000

int main()
{
	//  Feel free to use some white space or blank lines to make your program easier to read.
	float arr[MAX_SIZE];
	float quantity = 0;
	int i = 0, N = 0; // <--- No need for "k".
	float total = 0;

	printf("Please enter the number of the items ");
	scanf("%d", &N);

	for (i = 0; i < N; i++)
	{
		printf("Please enter the price of item %d ", i + 1);  // <--- Changed output.

		scanf("%f", &arr[i]);
	}

	//k = 0;
	for (i = 0; i < N; i++)
	{
		//k = k + 1;
		printf("Please enter the quantity of item %d  ", i + 1);  // <--- Changed output.
		scanf("%f", &quantity);
		total = total + quantity*arr[i];  // <--- Total was only getting one value for each itteration of the loop.
	}

	printf("the total amount is %.2f", total);  // <--- Changed output. ".2f" to print only 2 decimal places.
}


I have commented where I have made the changes. I believe you should understand. If not let me know.

line 21 is the problem only storing a single value for "total" each time through the loop instead of continuing to add to total. I am not sure yet if this would work in a C program total += quantity*arr[i];. It has been many years since I have worked with C, but I know it works in the C++ I use today.

See what you think.

Hope that helps,

Andy
Sorry for deleting the messages but this is like a part of a final test and i dont want my professor to find this topics because he will thinks of plagiarism.
concerning the script i will try it and give you an answer.
Thank you for your help
Your script is totally fine but the only issue that i have is that i want to print before the total something like :
(total number of items) items
quantity of item 1 * item 1 = price
quantity of item 2 * item 2 = price
...
Thank you for your help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
#include<math.h>

int main()
{
    enum { MAX_SIZE = 1000 };

	double price[MAX_SIZE] = {0} ;
	unsigned int quantity[MAX_SIZE] = {0} ;
	int num_items = 0 ;

	puts( "Please enter the number of the items: " ) ;
    scanf( "%d", &num_items ) ;
    if( num_items > MAX_SIZE ) num_items = MAX_SIZE ;

	for( int i = 0 ; i < num_items ; ++i ) {

        printf( "Please enter the price of item #%d: ", i+1 ) ;
        scanf( "%lf", price+i ) ; // note: %lf for scanf of double
        // validate if( price[i] < 0 ) ....

        printf( "Please enter the quantity of item #%d: ", i+1 ) ;
        scanf( "%u", quantity+i ) ;
	}

    double total = 0 ;

    puts( "\nbill of fare\n--------------\n" ) ;
    for( int i = 0 ; i < num_items ; ++i ) {

    	const double subtotal = quantity[i] * price[i] ;
    	printf( "%u * %.2f = %.2f\n", quantity[i], price[i], subtotal ) ;
    	total += subtotal ;
    }

	printf( "\nthe total amount is %.2f\n", total );
}
Hello Snow14,

i want to print before the total something like : (total number of items) items.

The short answer here is if you want the program to do something tell it to do do. I am thinking you would want to put something at line 24. Using what you have in your program the variable "N" would be useful.

Just a thought here, if you add a second array and maybe called the two arrays "price" and "quantity" you could fill both arrays in the first for loop and then use them in the second for loop to calculate the total. This way if you expand your program the arrays would be available for whatever you need.

Hope that helps,

Andy

Edit: When I said line 24 I do not know what I was thinking. It should be line 32.
Last edited on
Topic archived. No new replies allowed.