I need your help guys!

This is the problem.
Write a program that declares three single-dimensional arrays named miles, gallons, and mpg. Each array should be capable of holding 10 elements.

nothing is exactly working...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <stdio.h>
 
int main()
{
   float i, *mpg;
   
float miles[10]={240.5,300.0,189.6,310.6,280.7,216.9,199.4,160.3,177.4,192.3};
float gallons[10]={10.3,15.6,9.7,14,16.3,15.7,14.9,10.7,9.3,9.4};
   
   for (i=0;i<10;i++)
            *mpg == miles[0] / gallons[0];
            printf("Miles per gallon: %f", mpg);
      
      return 0;
}
One main problem here is that in your for loop, you want to be accessing each of the 10 elements of the array, but you defined *mpg so that it only calculates for the first element 10 times.
Another, you should declare i as an integer. Do this in the for loop.
mpg doesn't need to be a pointer, so I would get rid of the asterisk.

Only use one = for when you're assigning a value.

I believe you wanted:
1
2
3
4
5
   for (int i=0;i<10;i++){
            mpg = miles[i] / gallons[i]; // Now it accesses the ith element for each array.
            printf("Miles per gallon: %f", mpg);

   }

Don't forget the brackets for the for loop.

Also, I suggest adding a line break after you print the miles per gallon.


Since the assignment says to create three arrays, mpg should be an array.

To make this array, instead of printing the miles per gallon, you should set each element of an mpg[10] array to miles[i]/gallon[i] by using the fact mpg[i] = miles[i]/gallon[i].
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{
   float mpg[10];

float miles[10]={240.5,300.0,189.6,310.6,280.7,216.9,199.4,160.3,177.4,192.3};
float gallons[10]={10.3,15.6,9.7,14,16.3,15.7,14.9,10.7,9.3,9.4};

   for (int i=0;i<10;i++)
   {
            mpg[i] = miles[i] / gallons[i];
            printf("Miles per gallon: %f", mpg[i]);
   }
      return 0;
}
Line 11: You're attempting to set a pointer to the result of the calculation.
You're also using the equality operator, not the assignment operator.
Note that only line 11 will be executed within the for loop. If you want both lines 11 and 12 to execute within the loop, you need braces around both statements.

The assignment says to create three arrays. I don't see the third array.
At line 11, you probably want to be referencing miles[i] and gallons[i].


Last edited on
Alright so I got it to work but my program won't do what hypergrade wants, what am I missing? also huge thanks to those above for helping with the first question, y'all are amazing

What I currently have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main()
{
   float mpg[10];

float miles[10]={240.5,300.0,189.6,310.6,280.7,216.9,199.4,160.3,177.4,192.3};
float gallons[10]={10.3,15.6,9.7,14,16.3,15.7,14.9,10.7,9.3,9.4};

   for (int i=0;i<10;i++)
   {
            mpg[i] = miles[i] / gallons[i];
            printf("Miles Per Gallon\n");
            printf("      %2.1f\n", mpg[i]);
   }
      return 0;
}


picture of what my results are on hypergrade
http://snag.gy/Bux1J.jpg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main()
{
   float mpg[10];

float miles[10]={240.5,300.0,189.6,310.6,280.7,216.9,199.4,160.3,177.4,192.3};
float gallons[10]={10.3,15.6,9.7,14,16.3,15.7,14.9,10.7,9.3,9.4};
printf("Miles Per Gallon\n");//<<<<<<<put it here
   for (int i=0;i<10;i++)
   {
            mpg[i] = miles[i] / gallons[i];
            //printf("Miles Per Gallon\n");<<<<<<<
            printf("      %2.1f\n", mpg[i]);
   }
      return 0;
}
Chris you are a savior! I completely missed that, thank you!
Topic archived. No new replies allowed.