Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in C++ We take for granted all the place that we see arrays on a daily basis. On

ID: 3837184 • Letter: I

Question

in C++ We take for granted all the place that we see arrays on a daily basis. One that I know I'm sure familiar with is the one I interact with on my commute as I aimlessly scan through my radio dial looking for something, anything(!), to listen to on my way to work or home. Today you will create a radio interface that: Uses arrays to hold data for FM1, FM2, AM, and either a CD or MP3 folder (10 POINTS) each radio cluster will hold 6 radio entries the CD or MP3 folder will hold at least 10 songs Uses named constants to control the sizes of the arrays and the looping mechanisms (10 POINTS) Uses formatted output to properly display a radio control set and radio output (20 POINTS) Clearly shows the driver what is on the radio (the station at least) (10 of the 20 points) Clearly indicates to the driver how to interact (change stations) (the remaining 10 of the 20 points) Repaints the display so the driver can see what's going on (you can use system dependent code here) (5 POINTS) Function to allow the user to change / save a radio station into saved stations (20 POINTS) Loops as long as the driver has the car running and until the key is removed (option to kill radio) (5 POINTS

Explanation / Answer

#include<iostream>
#include<stdlib.h>
using namespace std;
#define SNG 10
#define FRE 6
//Function to validate the frequency
int valid(int FM[], int no)
{
//Initializes flag to zero
int flag = 0;
//Loops 6 times
for(int y = 0; y < FRE; y++)
{
//Checks the frequency number no entered by the user is available in the list or not
if(FM[y] == no)
{
//If available set the flag to one
flag = 1;
//Come out of the loop
break;
}//End of if
}//End of loop
//Returns the status of flag
return flag;
}//End of function

//Function to show
void show(int FM1[], int FM2[], string CD[], string source, int no)
{
cout<<" Now you are playing: ";
if(source == "FM1")
cout<<"FM1 band at frequency "<<no;
else if(source == "FM2")
cout<<"FM2 band at frequency "<<no;
else
cout<<"CD song "<<CD[no];
}//End of function

//Function to play
void play(int FM1[], int FM2[], string CD[])
{
char choice;
int band, fre, song;
int x, flag = 0;
//Accept choice for Radio or CD
cout<<" Enter R - Radio / C - CD: ";
cin>>choice;
//Checks the choice
switch(choice)
{
//For radio
case 'R': case 'r':
//Accepts the Bank FM1 or FM2
cout<<" Enter the Band 1 - FM1 2 - FM2: ";
cin>>band;
//Checks the bank
switch(band)
{
case 1:
//Displays the frequency
cout<<" Select the frequency for FM1 band: ";
for(x = 0; x < FRE; x++)
cout<<FM1[x]<<", ";
//Accepts the frequency
cout<<" Enter your choice: ";
cin>>fre;
//Validates the frequency is available in the list or not
//Function returns zero for not available
//One for available
if(valid(FM1, fre))
//If available display the data
show(FM1, FM2, CD, "FM1", fre);
else
//If not available show the error
cout<<" Invalid Frequency.";
break;
case 2:
//Displays the frequency
cout<<" Select the frequency for FM2 band: ";
for(x = 0; x < FRE; x++)
cout<<FM2[x]<<", ";
//Accepts the frequency
cout<<" Enter your choice: ";
cin>>fre;
//Validates the frequency is available in the list or not
//Function returns zero for not available
//One for available
if(valid(FM2, fre))
//If available display the data
show(FM1, FM2, CD, "FM2", fre);
else
//If not available show the error
cout<<" Invalid Frequency.";
break;
default:
cout<<" Invalid Choice ";
}//End of inner switch
break;
//For CD
case 'C': case 'c':
//Displays the songs
cout<<" Select the song number: "<<endl;
for(x = 0; x < SNG; x++)
cout<<"Song Number "<<(x+1)<<": "<<CD[x]<<endl;
//Accepts the song number
cout<<" Enter song number: ";
cin>>song;
//Validates the song number
if(song < 0 || song > SNG)
cout<<" Invalid Song Number.";
else
//If valid display the data
show(FM1, FM2, CD, "CD", song-1);
break;
default:
cout<<" Invalid Choice ";
}//End of outer switch
}//End of function

//Main function
int main()
{
//Declares and assigns data to FM!
int FM1[FRE] = {100, 110, 200, 250, 220, 300};
//Declares and assigns data to FM2
int FM2[FRE] = {130, 150, 190, 270, 280, 310};
//Declares and assigns data to CD
string CD[SNG] = {"Dil", "Rag", "Honey", "Danger", "Sur", "Sangam", "Heart", "My love", "Love", "Cold"};
char ch;
//Accepts user choice and plays it
play(FM1, FM2, CD);
//Loops till user choice
do
{
//Accepts user choice
cout<<" Enter C To change E to Exit";
cout<<" Enter your choice: ";
cin>>ch;
//Clears the screen
system("cls");
//Checks the choice
switch(ch)
{
//To change
case 'C': case 'c':
play(FM1, FM2, CD);
break;
//To exit
case 'E': case 'e':
exit(0);
break;
default:
cout<<" Invalid Choice";
}//End of switch
}while(1);//End of do - while loop
}//End of main

Sample Run:

Enter R - Radio / C - CD: r

Enter the Band
1 - FM1
2 - FM2: 1

Select the frequency for FM1 band: 100, 110, 200, 250, 220, 300,
Enter your choice: 310

Invalid Frequency.
Enter C To change
E to Exit
Enter your choice:C

Enter R - Radio / C - CD: r

Enter the Band
1 - FM1
2 - FM2: 1

Select the frequency for FM1 band: 100, 110, 200, 250, 220, 300,
Enter your choice: 200

Now you are playing: FM1 band at frequency 200
Enter C To change
E to Exit
Enter your choice:C

Enter R - Radio / C - CD: R

Enter the Band
1 - FM1
2 - FM2: 2

Select the frequency for FM2 band: 130, 150, 190, 270, 280, 310,
Enter your choice: 200

Invalid Frequency.
Enter C To change
E to Exit
Enter your choice:c

Enter R - Radio / C - CD: R

Enter the Band
1 - FM1
2 - FM2: 2

Select the frequency for FM2 band: 130, 150, 190, 270, 280, 310,
Enter your choice: 190

Now you are playing: FM2 band at frequency 190
Enter C To change
E to Exit
Enter your choice:C

Enter R - Radio / C - CD: C

Select the song number:
Song Number 1: Dil
Song Number 2: Rag
Song Number 3: Honey
Song Number 4: Danger
Song Number 5: Sur
Song Number 6: Sangam
Song Number 7: Heart
Song Number 8: My love
Song Number 9: Love
Song Number 10: Cold

Enter song number: 4

Now you are playing: CD song Danger
Enter C To change
E to Exit
Enter your choice: C

Enter R - Radio / C - CD: c

Select the song number:
Song Number 1: Dil
Song Number 2: Rag
Song Number 3: Honey
Song Number 4: Danger
Song Number 5: Sur
Song Number 6: Sangam
Song Number 7: Heart
Song Number 8: My love
Song Number 9: Love
Song Number 10: Cold

Enter song number: 12

Invalid Song Number.
Enter C To change
E to Exit
Enter your choice:C

Enter R - Radio / C - CD: c

Select the song number:
Song Number 1: Dil
Song Number 2: Rag
Song Number 3: Honey
Song Number 4: Danger
Song Number 5: Sur
Song Number 6: Sangam
Song Number 7: Heart
Song Number 8: My love
Song Number 9: Love
Song Number 10: Cold

Enter song number: 8

Now you are playing: CD song My love
Enter C To change
E to Exit
Enter your choice: E