Question

This should be programmed in C LANGUAGE! This lab requires you to use arrays, loop structures...

This should be programmed in C LANGUAGE!
This lab requires you to use arrays, loop structures and if statements/switch statements. You must use functions where indicated. Test your program often - make sure each bit of functionality works before you continue.

In this problem, we want to create a song and play it as we build it. The way we do this is by using arrays to store different notes. Notes are just tones that run for a specific amount of time. To play a note, you will use the beep function that is defined in the directive #include . Here is an example on how to use the function.

Beep(440, 2000); // Beeps at 440 Hz for 2 seconds

You will need to clear the screen for this program. You can do this by using system(“cls”).

Here is a description of what the program needs to do:

1. You want to create a program that displays a menu with three options:

a. Build the song

b. Play the song

c. Quit You must use a function to do this.

2. You then ask the user to enter a menu option. The program should treat upper and lower case values the same. For example a and A should be treated the same (Build the song).

3. If the user enters an invalid menu selection (ie it is not a or b or c), then display an error message and reprint the menu so that they can try again. HINT - you need a loop for this.

4. After option a or b is executed (ie you do what needs to be done for these options - see points 5 and 6), the menu should display again. The screen should be cleared before the menu is displayed again.

5. Each time the user selects option a, the program should ask them to enter a frequency (float) and a duration (int)1 . These values will be saved into the next available spot in two arrays - one for frequency and one for duration.

6. Assume the maximum number of notes that can be entered is 100. Do not allow the user to enter more than this.

7. When the user selects option b, the program will play the song that the user entered via option a.

8. Include your name and student number in your screen display.

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

Solution:

#include<stdio.h>

#include<windows.h>

// global variables

int i=0;

int duration[100];

float frequency[100];

//Function to insert the values into the array

void build()

{

    float freq;

    int t;

    printf("Enter the Frequency :");

    scanf("%f",&freq);

    frequency[i] = freq;

    printf("Enter the duration :");

    scanf("%d",&t);

    duration[i] =  t;

    i++;

}

//Function to play the notes

void play()

{

    int j;

    for(j=0;j<i;j++)

    {

        //cls will clear the screen for us

        system("cls");

        Beep(frequency[j],duration[j]);

        system("cls");       

    }

}

//Function to quit the program

void quit()

    {

        //exit() function will terminate the program

        exit(0);

    }


int main()

{    

    char choice;

    while(1)

    {

        //Providing menu and getting the choice

        printf("a. Build the song\nb. Play the song\nc. Quit You must use a function to do this\n");

        scanf("%c",&choice);

        

        //Using switch statements

        switch (choice)

        {

            case 'a':

                build();

                break;

            case 'A':

                build();

                break;

            case 'b':

                play();

                break;

            case 'B':

                play();

                break;

            case 'c':

                quit();

                break;

            case 'C':

                quit();

                break;

            default:

            //Printing if there is some wrong options is given

                printf("Chose the valid option\n");

                sleep(0.5);

                break;

        }  

            system("cls");

    }

}

Explanation :
  
   Initialize three global variables int , two arrays one is int array to store duration and float array to store the frequency
   Create and define three functions build , play , quit
       build() function will ask user to enter the frequency and duration of a note and store in the frequency array and duration array
       play() function will play the given notes for this we have to use Beep() function which takes two arguments frequency and duration you should include windows.h library for this function
       quit() function simply terminates the execution of program by using exit() function
      
   main function
       Here initialize a char variable
       Use while loop with 1 as condition this make loop run infinitely also we can use for loop as for(;;)
       Print the menu at each iteration and update the char variable
       using switch statements do the right opertion
           if choice is a or A then execute build()
           if choice is b or B then execute play()
           if choice is c or C then execute quit()
           if choice is not above mentioned prompt user to enter the right option and wait for 0.5 seconds , waiting can be done by using sleep() function
       At last clear the screen

NOTE:

Step 8 can be done using printf statements

Global variables can be accessed anywhere in the program if any variables even though initialized in main function it can be used only in main function that is the reason frequency array and i and duration array are used as global variables

Output:

Please give a thumbs up !!

Add a comment
Know the answer?
Add Answer to:
This should be programmed in C LANGUAGE! This lab requires you to use arrays, loop structures...
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
  • DO NOT USE ARRAYS IF YOU KNOW ABOUT ARRAYS, I WANT YOU TO USE 3 VARIABLES,...

    DO NOT USE ARRAYS IF YOU KNOW ABOUT ARRAYS, I WANT YOU TO USE 3 VARIABLES, IT WILL FORCE YOU TO USE COMPOUND LOGICAL STATEMENTS FOR THE IF STATEMENTS. Create a program that will display a menu to the user. The choices should be as follows: Enter 3 grades Show average (with the 3 grades) and letter grade Show highest grade Show lowest grade Exit If you want to put the menu into a function you may. The program should...

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • Overview: You will be writing classes that implement a playlist simulation for a music streaming app.The...

    Overview: You will be writing classes that implement a playlist simulation for a music streaming app.The Playlist class will contain a dynamic array of Song objects, and the Song class contains several pieces of information about a song. You will need to: 1) Finish the writing of two classes: Song and Playlist. The full header file for the Song class has been provided in a file called Song.h. 2) Write a main program (filename menu.cpp) that creates a single Playlist...

  • Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays,...

    Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...

  • CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

    CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...

  • The language is C++ for visual studio express 2013 for windows create a TicketManager class and...

    The language is C++ for visual studio express 2013 for windows create a TicketManager class and a program that uses the class to sell tickets for a performance at a theater. Here are all the specs. This is an intense program, please follow all instructions carefully. -I am only creating the client program that uses the class and all it's functions -The client program is a menu-driven program that provides the user with box office options for a theater and...

  • The purpose of this assignment is to get experience with an array, do while loop and...

    The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...

  • Write a menu driven program to demonstrate the use of array and switch. It must be...

    Write a menu driven program to demonstrate the use of array and switch. It must be written in C language . Here is the menu - Press 1 to add a number to the array. Press 2 to display the numbers entered in the array so far Press 3 to find the average of the numbers entered. Press 4 to exit. Option 1 - The user should be able to enter 20 numbers only. If all twenty numbers are entered...

  • Please solve using c++ plus There are two text files with the following information stored in...

    Please solve using c++ plus There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a C++ program that reads these text files and provides user with the following menu: 1. Enter the instructor ID...

  • C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to...

    C++ Implement Random Access Binary Files. Do Not use Arrays instead write the data directly to Random Access Binary File. New program should be modified to display a menu for the user to do the following: 1. Replace Employee and Department classes with Employee and Department Structures. 2. Inside each structure, replace all string variables with array of characters. 3. Make Employee and Department editable. That means, the user should be able to edit a given Employee and Department. 4....

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