I'm making a program that will let me read an integer entered at the terminal via the scanf
function, and generate its assigned value from the array that has been created already. Here is the program's description:
Your program must read 10 integers (the quantities needed for each of the ingredients, in order) and store them in an array. It should then read an integer which represents an ingredient's ID number (between 0 and 9), and output the corresponding quantity.
#include <stdio.h>
int main () {
int array[10];
int store = 0;
int i = 0;
printf("write how much the ingredients weigh in grams\n");
for (i=0; i<10; i++) {
scanf("%d", array);
printf("%i weighs %dg\n", i, array[store]);
}
printf("Which ingredient ID do you want to check the weight of? ");
int ingredient = 0;
scanf("%d", &store);
printf("The weight of this ingredient is: %d\n", ingredient[array]);
}
For example, if the values stored in the array are: 23, 54, 12, 51, 11, 10, 99, 32, 9, 14 then I would expect the program to print '12' when I type '2' after being prompted with the question "Which ingredient id do you want to check the weight of?" However, at the moment when I type the number '6' for example, it will generate the value assigned to the number '8'. So it will generate '9' instead of '99'.
How can I fix this?