Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void add_presents()
- {
- int i=0;
- char PresentArray_name[10][30];//A 2D array to store names of presents,
- which can only be 30 characters long
- int PresentArray_price[10];
- printf("This area is a little difficult to navigate as the answer for
- the question is stored before the question is displayed! Simply type in the
- toy hit the enter key, and then input it's price then hit the enter key!
- Don't pay attention to the headings!n");
- for (i=0;i<10;i++)
- {
- printf("Enter present %d:n", i+1);
- scanf("%s", &PresentArray_name[i]);//Stores the presents in the
- PresentArray_name
- printf("Enter price for present %d:n", i+1);
- scanf("%d", &PresentArray_price[i]);//Stores the presents in the
- PresentArray_price
- if (PresentArray_price[i]<5||PresentArray_price[i]>15)
- {
- printf("Invalid! Enter a price between 5 and 15:n");
- scanf("%d", &PresentArray_price[i]);
- }
- }
- for (i=0;i<10;i++)
- {
- printf("Present %s costs %dn", PresentArray_name[i],
- PresentArray_price[i]);//Prints the names and the costs of each present
- }
- srand(time(NULL));
- time_t t;
- srand((unsigned)(&t));
- for (i=0;i<total_number_of_presents_required;i++)//Loop counter form
- another part of the code
- {
- printf("Kid: %d gets %s, which costs: %dn",i+1,
- PresentArray_name[rand()%10], PresentArray_price[i]);
- }
- }
- #include <stdio.h>
- #include <time.h>
- struct Present
- {
- char name[30]; // note this will only hold a name 29 long to ensure room for NUL terminator
- int price;
- };
- int main(void)
- {
- srand(time(NULL));
- // now, you can simply create an array of this struct, and the name
- // and price will always be together
- struct Present presents[10];
- int i;
- for (i=0; i<10; i++)
- {
- // get user input. You should also check the return value of scanf which I'm omitting here for brevity
- // get the present name
- scanf("%s", presents[i].name);
- // get the price
- scanf("%d", &(presents[i].price));
- }
- // now when you randomly select a present, you'll get it's name and price
- i = rand()%10;
- printf("present name: %sn", presents[i].name);
- printf("present price: %dn", presents[i].price);
- return 0;
- }
Add Comment
Please, Sign In to add comment