Thread: Need help with array calculation

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    9

    Need help with array calculation

    Hello everyone,

    I am trying do a simple calculation using (2) arrays a for loop and a counter however I am not receiving the print results that I am looking for.

    My intent is to take a prepopulated array with figure amounts and multiple them times an interest rate. display that amount then store this amount in another array.

    I wanted to use a for loop since I have all ready demensioned the array. While populating the temp array I want to total the sum of all of the items processed.

    Could someone look at my code and maybe point me in the right direction?

    Code:
    #include <stdio.h>
    
    main()
    
    {
    
    //Set store tax variables
    float StoreTax1 = 7.25;
    
    //Declare ProductPrice array
    float ProductPrice[10]={4.25, 3.00, 1.25, 5.50, 2.50, 7.00, 8.00, 9.00, 8.50, 11.00};
    float Store1TaxPerItem[10];
    float Store1;
    int x = 0;
    
    printf("Begin process");
    
    for (x = 0; x < 9; x++ )
    		
    	Store1TaxPerItem[x] = ProductPrice[x] * (StoreTax1/100);
    	printf("The result Store1TaxPerItem %f\n", Store1TaxPerItem[x]);
    
    	Store1 = Store1 + Store1TaxPerItem[x];
    	printf("The result is %f\n", Store1);
    
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by magiccity77 View Post
    Hello everyone,

    I am trying do a simple calculation using (2) arrays a for loop and a counter however I am not receiving the print results that I am looking for.

    My intent is to take a prepopulated array with figure amounts and multiple them times an interest rate. display that amount then store this amount in another array.

    I wanted to use a for loop since I have all ready demensioned the array. While populating the temp array I want to total the sum of all of the items processed.

    Could someone look at my code and maybe point me in the right direction?

    Code:
    #include <stdio.h>
    
    main()
    
    {
    
    //Set store tax variables
    const float StoreTax1 = 0.0725;
    
    //Declare ProductPrice array
    float ProductPrice[10]={4.25, 3.00, 1.25, 5.50, 2.50, 7.00, 8.00, 9.00, 8.50, 11.00};
    float Store1TaxPerItem[10];
    float Store1;
    int x = 0;
    
    printf("Begin process");
    
    for (x = 0; x < 9; x++ )
    {		
    	Store1TaxPerItem[x] = ProductPrice[x] * StoreTax1;
    	printf("The result Store1TaxPerItem &#37;f\n", Store1TaxPerItem[x]);
    
    	Store1 = Store1 + Store1TaxPerItem[x];
    }
    printf("The result is %f\n", Store1);
    
    }
    This isn't python, sport.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by magiccity77 View Post
    Code:
    for (x = 0; x < 9; x++ )
    	Store1TaxPerItem[x] = ProductPrice[x] * (StoreTax1/100);
    }
    All of your problems can be found here. I suggest you do that for loop on a piece of paper to see what's going on.
    Last edited by kpreston; 09-29-2008 at 06:19 PM. Reason: Edit: Plus what was stated above :P

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by kpreston View Post
    All of your problems can be found here. I suggest you do that for loop on a piece of paper to see what's going on.
    Are you meaning to only store the sales tax? Or are you wanting to store the value after tax? And there is a huge difference between a tax and interest.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Two problems:
    1 - Your loop only goes from 0 to 8 but you have 10 elements.
    2 - You didn't initialize Store1 to zero.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    I want to store the tax for each item. then total up the tax amounts for each item.

    So if I have an item that is 5.00 and the tax rate is 7.25% then all i want to do is multiple the two to get that items rate. Take that rate and add it some variable then sum up all of the items for that given run. In this case I want to go through the ten items or prices listed then total them up while looping.

    and print to screen for the user to see the final result.

    Eventually I will change it to user inputs but wanted to get something working first. This is my second day learning how to write in C. (VB.NET developer trying to learn C.)

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by nonoob View Post
    Two problems:
    1 - Your loop only goes from 0 to 8 but you have 10 elements.
    2 - You didn't initialize Store1 to zero.
    Thanks for pointing this out. I want 11 elements and should have set x<10
    and I have set Store1 = 0

    Thanks this is very helpful.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    #include <stdio.h>
    
    int main(void )
    {
      //Set store tax variables
      const float StoreTax1 = 0.0725;
    
      //Declare ProductPrice array
      float ProductPrice[]={4.25, 3.00, 1.25, 5.50, 2.50, 7.00, 8.00, 9.00, 8.50, 11.00};
      float Store1TaxPerItem[10];
      float Store1 = 0.0f; // thank you nonoob
      int x = 0;
    
      printf("Begin process");
    
      for (x = 0; x < sizeof(ProductPrice)/sizeof(*ProductPrice); x++ )
      {		
        Store1TaxPerItem[x] = ProductPrice[x] * StoreTax1;
        printf("The result Store1TaxPerItem &#37;f\n", Store1TaxPerItem[x]);
    
        Store1 = Store1 + Store1TaxPerItem[x];
      }
      printf("The result is %f\n", Store1);
    }

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    9

    Talking

    Quote Originally Posted by master5001 View Post
    Example:
    Code:
    #include <stdio.h>
    
    int main(void )
    {
      //Set store tax variables
      const float StoreTax1 = 0.0725;
    
      //Declare ProductPrice array
      float ProductPrice[]={4.25, 3.00, 1.25, 5.50, 2.50, 7.00, 8.00, 9.00, 8.50, 11.00};
      float Store1TaxPerItem[10];
      float Store1 = 0.0f; // thank you nonoob
      int x = 0;
    
      printf("Begin process");
    
      for (x = 0; x < sizeof(ProductPrice)/sizeof(*ProductPrice); x++ )
      {		
        Store1TaxPerItem[x] = ProductPrice[x] * StoreTax1;
        printf("The result Store1TaxPerItem %f\n", Store1TaxPerItem[x]);
    
        Store1 = Store1 + Store1TaxPerItem[x];
      }
      printf("The result is %f\n", Store1);
    }

    Thanks master5001.

    One final question am I using the print statement correctly? When msdos runs the screen is blank and nothing seems to respond. A little frustrating for me but I enjoy learning.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You know what, I just noticed another potential issue with the code I posted.

    Change Store1TaxPerItem[10] to this:

    Example:
    Code:
    float *Store1TaxPerItem = malloc(sizeof(ProductPrice));
    
    // And before the first printf() add this
    if(!Store1TaxPerItem)
      return EXIT_FAILURE;
    
    // And after the last printf() add this
    free(Store1TaxPerItem);
    return EXIT_SUCCESS;
    You will need to include <stdlib.h> for some of the macros I used.

    You are using your printf()'s correctly. I don't truthfully see why it would hang.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    On second thought, I best just put the code with the corrections to be most clear.

    Example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      //Set store tax variables
      const float StoreTax1 = 0.0725;
    
      //Declare ProductPrice array
      float ProductPrice[]={4.25, 3.00, 1.25, 5.50, 2.50, 7.00, 8.00, 9.00, 8.50, 11.00};
      float *Store1TaxPerItem = malloc(sizeof(ProductPrice));
      float Store1 = 0.0f; // thank you nonoob
      int x = 0;
    
      if(!Store1TaxPerItem)
      {
        fputs("Uh oh, spighetti-o! Out of memory! Please go purchase more.\n", stderr);
        return EXIT_FAILURE;
      }
    
      printf("Begin process");
    
      for (x = 0; x < sizeof(ProductPrice)/sizeof(*ProductPrice); x++ )
      {		
        Store1TaxPerItem[x] = ProductPrice[x] * StoreTax1;
        printf("The result Store1TaxPerItem &#37;f\n", Store1TaxPerItem[x]);
    
        Store1 = Store1 + Store1TaxPerItem[x];
      }
      printf("The result is %f\n", Store1);
      return EXIT_SUCCESS;
    }

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    In reality, what may have happened is a buffer overflow since I tried to make things as "automatic" as possible. The downside of doing things that way was that your tax array was not so "automatically" sized, thus you may have overflowed right into the Store1 variable. Though this still wouldn't have necessarily caused an infinite loop, it is a possible thing that was wrong with the other code.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by master5001 View Post
    On second thought, I best just put the code with the corrections to be most clear.

    Example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      //Set store tax variables
      const float StoreTax1 = 0.0725;
    
      //Declare ProductPrice array
      float ProductPrice[]={4.25, 3.00, 1.25, 5.50, 2.50, 7.00, 8.00, 9.00, 8.50, 11.00};
      float *Store1TaxPerItem = malloc(sizeof(ProductPrice));
      float Store1 = 0.0f; // thank you nonoob
      int x = 0;
    
      if(!Store1TaxPerItem)
      {
        fputs("Uh oh, spighetti-o! Out of memory! Please go purchase more.\n", stderr);
        return EXIT_FAILURE;
      }
    
      printf("Begin process");
    
      for (x = 0; x < sizeof(ProductPrice)/sizeof(*ProductPrice); x++ )
      {		
        Store1TaxPerItem[x] = ProductPrice[x] * StoreTax1;
        printf("The result Store1TaxPerItem %f\n", Store1TaxPerItem[x]);
    
        Store1 = Store1 + Store1TaxPerItem[x];
      }
      printf("The result is %f\n", Store1);
      return EXIT_SUCCESS;
    }
    I received this error:

    line 7: Parse Error, expecting `','' or `SEP'
    'float StoreTax1 = 0.0725'
    aborting compile
    This occured after I tried to run it. I thought that after each line of code that I finish that I am supposed to end with a semicolon so why is it requesting a comma or seperator?

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am not sure.... You could put 0.0725f; but that would likely result in the same error. Comment out the second include. Maybe there is something that unintentionally got changed in your stdlib header.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Quote Originally Posted by master5001 View Post
    I am not sure.... You could put 0.0725f; but that would likely result in the same error. Comment out the second include. Maybe there is something that unintentionally got changed in your stdlib header.
    Ok to resolve that issue I did the following:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      //Set store tax variables
      float StoreTax1;
      StoreTax1 = 0.0725f;
    
      //Declare ProductPrice array
      float ProductPrice[] = {4.25, 3.00, 1.25, 5.50, 2.50, 7.00, 8.00, 9.00, 8.50, 11.00};
      float *Store1TaxPerItem = malloc(sizeof(ProductPrice));
      float Store1 = 0.0f; // thank you nonoob
      int x = 0;
    
      if(!Store1TaxPerItem)
      {
        fputs("Uh oh, spighetti-o! Out of memory! Please go purchase more.\n", stderr);
        return EXIT_FAILURE;
      }
    
      printf("Begin process");
    
      for (x = 0; x < sizeof(ProductPrice)/sizeof(*ProductPrice); x++ )
      {		
        Store1TaxPerItem[x] = ProductPrice[x] * StoreTax1;
        printf("The result Store1TaxPerItem %f\n", Store1TaxPerItem[x]);
    
        Store1 = Store1 + Store1TaxPerItem[x];
      }
      printf("The result is %f\n", Store1);
      return EXIT_SUCCESS;
    }
    Now I have a new issue. Here is the error message.

    line 11: Parse Error, expecting `'}''
    'float ProductPrice[] = {4.25, 3.00, 1.25, 5.50, 2.50, 7.00, 8.00, 9.00, 8.50, 11.00}'
    aborting compile

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM