# Thread: Need help with array calculation

1. ## 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. Originally Posted by magiccity77
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. Originally Posted by magiccity77
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.

4. Originally Posted by kpreston
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. 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. 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. Originally Posted by nonoob
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

8. 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. Originally Posted by master5001
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. 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. 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. 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. Originally Posted by master5001
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;
}```

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. 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. Originally Posted by master5001
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