Question

Last assignment: C programming Submission must include: a function, an array, and a menu system Goal...

Last assignment: C programming

Submission must include: a function, an array, and a menu system

Goal is to create a program that will generate a random exercise for the target area selected and the number of times it will be done (all of the options except cardio) or the amount of time doing the task.(the cardio option), but I'm having trouble figuring out how to generate a random option using an array.

This is what I have so far...

#include <stdio.h>
#include <stdlib.h>

void WelcomeMessage()
{
   printf("Welcome to the workout generator!\nGet ready to stay fit!!\n");
}

main ()
{
   char array1[5]; // 1. Cardio workout options
   array1[0] = "Jump roping";
   array1[1] = "Running";
   array1[2] = "Cycling";
   array1[3] = "Swimming";
   array1[4] = "Dancing";

   char array2[5]; // 2. Arm workout options
   array2[0] = "Chest press";
   array2[1] = "Bicep curls";
   array2[2] = "Side curls";
   array2[3] = "Front extensions";
   array2[4] = "Push ups";

   char array3[5]; // 3. Leg workout options
   array3[0] = "Lunges";
   array3[1] = "Scissor jumps";
   array3[2] = "Step ups";
   array3[3] = "Leg lifts";
   array3[4] = "Climbers";

   char array4[5]; // 4. Abdomen workout options
   array4[0] = "Crunches";
   array4[1] = "Sit ups";
   array4[2] = "Scissors";
   array4[3] = "Leg bridges";
   array4[4] = "Burpees";

   char array5[5]; // 5. Glutes workout options
   array5[0] = "Squats";
   array5[1] = "Donkey kicks";
   array5[2] = "Bridges";
   array5[3] = "Hip thrusts";
   array5[4] = "Deadlifts";
  
   // Other variables
   int choice;

   WelcomeMessage();
   printf("\n");

   // Menu
   printf("Exercise Generator:\n");
   printf("1. Cardio\n");
   printf("2. Arms\n");
   printf("3. Legs\n");
   printf("4. Abdomen\n");
   printf("5. Glutes\n");
   printf("6. Exit\n");

   printf("\nEnter your choice of workout:\n");
   scanf_s("i", &choice);

   do
   {
       switch (choice)
       {
       case 1:

           break;

       case 2:
           break;

       case 3:
           break;

       case 4:
           break;

       case 5:
           break;

       case 6:
           return 0;
       }

       printf("\nEnter your choice:\n");

       scanf_s("i", &choice);

   } while (1);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The given problem can be solved by using the rand() function which is predefined in the "stdlib.h" header file. The rand() function generates a random number. What we have to do is, set a maximum value, and then use the modulus operator. That means we are limiting the range of the random values generated.

For example: If we set the maximum value to 4, then by the follwing operation

value=rand()%4;

We are limiting the values in the range 0-3;

In this way in the given problem if we set the maximum value to 5 then the range will be 0-4, which are the required indexes of the array, and thus the required range.

The program has sum errors in it. The corrected program with execution of the rand() function is as follows.

Program:

#include <stdio.h>
#include <stdlib.h>

void WelcomeMessage()
{
printf("Welcome to the workout generator!\nGet ready to stay fit!!\n");
}

main ()
{
char array1[5][20] = {"Jump_Roping","Running","Cycling","Swimming","Dancing"}; // 1. Cardio workout options
char array2[5][20] = {"Chest_Press","Bicep_Curls","Side_Curls","Front_Extensions","Push_Ups"}; // 2. Arm workout options
char array3[5][20] = {"Lunges","Scissor_Jumps","Step_Ups","Leg_Lifts","Climbers"}; // 3. Leg workout options
char array4[5][20] = {"Crunches","Sit_Ups","Scissors","Leg_Bridges","Burpees"}; // 4. Abdomen workout options
char array5[5][20] = {"Squats","Donkey_Kicks","Bridges","Hip_Thrusts","Deadlifts"}; // 5. Glutes workout options
int choice,random;

WelcomeMessage();
printf("\n");

do
{
// Menu
printf("\nExercise Generator:\n");
printf("1. Cardio\n");
printf("2. Arms\n");
printf("3. Legs\n");
printf("4. Abdomen\n");
printf("5. Glutes\n");
printf("6. Exit\n");
printf("\nEnter your choice of workout:\n");
scanf("%d", &choice);
random=rand()%5;
switch (choice)
{
case 1: printf("%s for 1 hour",array1[random]);
break;

case 2: printf("%s 15 times",array2[random]);
break;

case 3: printf("%s 15 times",array3[random]);
break;

case 4: printf("%s 15 times",array4[random]);
break;

case 5: printf("%s 15 times",array1[random]);
break;

case 6: exit(0);

default : printf("Wrong Choice.");
}
}while(choice!=6);

}

Screenshot of the Code:

#include <stdio.h> #include <stdlib.h> void WelcomeMessage() printf(Welcome to the workout generator!\nGet ready to stay fitprintf(\nEnter your choice of workout:\n); scanf(%d, &choice); random=rand() $5; switch (choice) case 1: printf(%s for 1

Output 1:

Welcome to the workout generator! Get ready to stay fit!! Exercise Generator: 1. Cardio 2. Arms 3. Legs 4. Abdomen 5. Glutes

Output 2:

Welcome to the workout generator! Get ready to stay fit!! Exercise Generator: 1. Cardio 2. Arms 3. Legs 4. Abdomen 5. Glutes

Add a comment
Know the answer?
Add Answer to:
Last assignment: C programming Submission must include: a function, an array, and a menu system Goal...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++, need help thank you Given an array of ints, return the sum of all elements...

    c++, need help thank you Given an array of ints, return the sum of all elements from the array that come before the first element that equals number 4 in the array. The array will contain at least one 4. Function prototype: int pre4int array ( ], int size); Hint: to find the array size, use: int size = sizeof(array) / sizeof( array[0]); Sample runs: int array1[ ] = {1, 2, 4, 1); pre4(array1, 4); // returns 3 int array2...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • Identify and list all the User defined Functions to be used in the system #include <stdio.h>...

    Identify and list all the User defined Functions to be used in the system #include <stdio.h> ///for input output functions like printf, scanf #include <stdlib.h> #include <conio.h> #include <windows.h> ///for windows related functions (not important) #include <string.h> ///string operations /** List of Global Variable */ COORD coord = {0,0}; /// top-left corner of window /** function : gotoxy @param input: x and y coordinates @param output: moves the cursor in specified position of console */ void gotoxy(int x,int y) {...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • int main() { int a, Band3 = 0, Band1, Band2, b, c = 1; float t;...

    int main() { int a, Band3 = 0, Band1, Band2, b, c = 1; float t; while (c) { Band3 = 0; printf(" Enter Resistance Value: "); scanf_s("%d", &a); printf("\n Enter Tolerance Value: "); scanf_s("%f", &t); b = t * 100; while (a % 10 == 0) { Band3++; a = a / 10; } Band2 = a % 10; a = a / 10; Band1 = a % 10; printf("\n Band1 = %d, Band2 = %d, Band3 = %d...

  • I need little help with C language. I need to pass what I get from HexToBin(char*...

    I need little help with C language. I need to pass what I get from HexToBin(char* hexdec) into char* input so that what I got there it should pass it as string array parametr. Example: Enter IEEE-Hex: 40200000 Equivalent Binary value is : 01000000001000000000000000000000 Decimal Number: 2.5 #include <stdio.h> void HexToBin(char* hexdec) {    long int i = 0;    while (hexdec[i]) {        switch (hexdec[i]) {        case '0':            printf("0000");            break;...

  • The code snippet below is part of the restaurant menu program you previously used in the...

    The code snippet below is part of the restaurant menu program you previously used in the lab. Add a choice for a drink. Add the necessary code to allow the program to calculate the total price of order for the user. Assume the following price list: Hamburger $5 Hotdog $4 Fries $3 Drink $2 The program should allow the user to keep entering order until choosing to exit. At the end the program prints an order summary like this: You...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT