Thread: Array multiply

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    31

    Array multiply

    Can you please help me to print Value?

    Value = number of units * unit price;

    Code:
    #include<stdio.h>
    #include<conio.h>
    #define MAX 5
    int printinventory(int , int unit[] , float price[]);
    int main()
    {
    	int item[MAX],unit[MAX],x,i;
    	float price[MAX];
    	printf("Please enter how many category items (up to 5) : ");
    	scanf("%d",&x);
    	for(i=0;i<x;i++)
    	{
        printf("\nPlease enter Number of Units #%d : ",i+1);
    	scanf(" %d",&unit[i]);
    	printf("\nPlease enter the Unit Price #%d : ",i+1);
    	scanf(" %f",&price[i]);
    	}
    	printinventory(x , unit , price);
    	getch();
    }
    
    
    int printinventory (int  y, int unit[] , float price[])
    {
    	int i,j=0;
    	float value[5];
    	for(i=0;i<y;i++);
    	{
    		value[i] = (unit[i]) * (price[i]);
    	}
    	printf("Item     Nimber of Units   Unit Price    Value ");
    	printf("\n------------------------------------------------");
    	for(i=1;i<=y;i++)
    	{
    		printf("\n%d",i);
    		printf("\t%d",unit[j]);
    		printf("\t\t$%.2f",price[j]);
    		printf("\t\t\%.2f",value[j]);
    		j++;
    	}
    	getch();
    }

  2. #2
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    i reccomend using flushing input:

    int ch;
    while((ch = getchar)) != '\n' && ch != EOF);


    after the scanf.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @loserone+_+: That doesn't apply to this example, it only applies when "scanf()" is used to read a character(s). Here, only integers and floats are being scanned, and whitespace is ignored in those instances.

    @OP: Compare the "for()" loops in lines 11 and 33 versus the "for()" loop in line 27. Notice how the latter is ending in a semi-colon. That is wrong.

    To explain further, a semi-colon by itself is considered a null statement, which means nothing happens. So your line 27 can be shown as:

    Code:
        for(i=0;i<y;i++)
            ;  // <--- for each iteration of the loop, nothing is happening.
        // <--- then the following block is execute ONCE afterwards - however, 'i' is already one past a valid value,
        //      due to the last incrementation in the "for()" loop
        {
            value[i] = (unit[i]) * (price[i]);
        }
    Solution - get rid of that semi-colon. In fact, if the user enters the maximum value of category items, the statement in brackets will overrun the bounds of the array.

    Code:
        int j = 0;
    
        // ...
    
        for(i=1;i<=y;i++)
        {
            printf("\n%d",i);
            printf("\t%d",unit[j]);
            printf("\t\t$%.2f",price[j]);
            printf("\t\t\%.2f",value[j]);
            j++;
        }
    This seems like a very round-about way of printing your output. You want "item" to start at one, but your arrays to start at zero, so you use two variables. Why not simply use the same scheme you did in "main()" and print the items as "i + 1"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiply 2 array's ?
    By Omar Do in forum C Programming
    Replies: 12
    Last Post: 03-16-2012, 08:26 PM
  2. multiply by 7
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 05-22-2006, 02:19 PM
  3. program won't multiply right
    By Will_rookwood in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2006, 07:28 PM
  4. Multiply it by 10 then Int to Hex
    By Mr. Acclude in forum C Programming
    Replies: 16
    Last Post: 09-23-2004, 08:15 PM
  5. multiply int..
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-14-2002, 08:23 AM