Create an array called myList. Write a function called FirstElements for adding
ID: 3851585 • Letter: C
Question
Create an array called myList.
Write a function called FirstElements for adding 100 random integers between 100 to 1000 to myList.
Initialize the first 100 elements of myList by calling the FirstElements function.
Create a menu to display the following options and a number corresponding to each option which is used for selecting the desired option:
1- adding any amount of numbers to myList
2- removing all occurrences of a selected number from myList
3- finding and displaying the maximum, minimum and range (maximum-minimum) in myList
4- finding the number of occurrences of a number in myList
5- doubling (multiplying by 2) all numbers in myList
6- display all members of myList.
7- quit
for each menu item create a function.
when the item number is typed, the function should be executed, the required inputs should be requested, and the proper output should be produced.
the outputs can be formatted in a proper fashion of your choice
after executing any function in the menu, the user should be given the choice to select another menu or quit (Y for the new request and Q for quit).
if the user has a new request, the menu should appear again and the process repeated.
Note: In one of the codes that I have shared before, you can find an example of creating a menu.
This is to be done in C programming. Thank you. I have no clue how to start it.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int myList[100];
void firstElements()
{
int randomnumber;
int i=0;
for(i=0;i<100;i++)
{
randomnumber = 100+ rand() % 1000;
myList[i]=randomnumber;
}
}
void adding()
{
int number,i;
printf("Enter the number to add to all elements in the array");
scanf("%d",&number);
for(i=0;i<100;i++)
{
myList[i]=myList[i]+number;
}
}
void removing()
{
int number,i;
printf("Enter the number to remove all the occurence of this element");
scanf("%d",&number);
for(i=0;i<100;i++)
{
if(myList[i]==number)
{
myList[i]=0;
}
}
}
void maxmin()
{
int max=myList[0],min=myList[0],i;
for(i=0;i<100;i++)
{
if(max<myList[i])
{
max=myList[i];
}
else if(min>myList[i])
{
min=myList[i];
}
}
printf("Max element is %d",max);
printf("Min element is %d",min);
}
void occurence()
{
int i,count=0,number;
printf("Enter the number to find number of occurences in myList");
scanf("%d",&number);
for(i=0;i<100;i++)
{
if(myList[i]==number)
{
count++;
}
}
printf("%d is occured %d of time in myList",number,count);
}
void doubling()
{
int i;
for(i=0;i<100;i++)
{
myList[i]=myList[i]*2;
}
}
void display()
{
int i;
for(i=0;i<100;i++)
{
printf("%d ",myList[i]);
}
}
void quit()
{
exit(0);
}
void main()
{
int choice;
firstElements();
printf("1- adding any amount of numbers to myList ");
printf("2- removing all occurrences of a selected number from myList ");
printf("3- finding and displaying the maximum, minimum and range (maximum-minimum) in myList ");
printf("4- finding the number of occurrences of a number in myList ");
printf("5- doubling (multiplying by 2) all numbers in myList ");
printf("6- display all members of myList. ");
printf("7- quit ");
printf("Enter your choice");
scanf("%d",&choice);
swith(choice)
{
case 1:
adding();
break;
case 2:
removing();
break;
case 3:
maxmin();
break;
case 4:
occurence();
break;
case 5:
doubling();
break;
case 6:
display();
break;
case 7:
quit();
break;
case default:
printf("Invalid input");
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.