objective: To show our understanding of arrays, specifically how to declare, ini
ID: 3824275 • Letter: O
Question
objective: To show our understanding of arrays, specifically how to declare, initialize and manipulate the elements of an array. To further our understanding of functions, loops, conditional statements, data types and variables. Assignment: Write a program that uses an array to store a series of integers. The program should prompt the user to enter elements into the array. After the array has been filled, it should display the initial contents of the array. It should then perform operations to manipulate the elements of the array through specific functions. Note that if the annay is modified in any way, the program should re-display the contents of the array. Procedure: 1. Declare an array of integers of some pre-defined maximum size. You are free to decide the size of the array, but all the elements of the array should be initialized to zero. 2. Implement the following operations on the array through the specified functions: o input() This function prompts the user to populate the array with data entered from the keyboard. As this is an array of integers, the data should all be numeric integers. o display() This function outputs the contents of the array one element at a time. The array should be output using the following format: Postion Value 10 Note: position, represents the logical index of the array and value represents the numeric integer stored in the corresponding physical location. In other words position 1 corresponds to the value at arraylOExplanation / Answer
#include<iostream>
using namespace std;
int a[10];
int elements = 0;
double mean=0;
int sum=0;
int min=0;
int max=0;
void menu()
{
cout << "1.Modify 2.replace 3.clear 4.search 5.count 6.quit what do you choose??";
}
void statistics()
{
min = a[0];
for (int i = 0; i < elements; i++)
{
sum = sum + a[i];
if (min > a[i])
{
min = a[i];
}
if (max < a[i])
{
max = a[i];
}
}
mean = sum / elements;
}
int count(int value)
{
int times = 0;
for (int i = 0; i < elements; i++)
{
if (a[i] == value)
{
times++;
}
}
return times;
}
int search(int value)
{
int pos = -1;
for (int i = 0; i < elements; i++)
{
if (a[i] == value)
{
pos = i;
break;
}
}
return pos;
}
void modify(int logicalpos,int newvalue)
{
int pos = logicalpos - 1;
a[pos] = newvalue;
}
int replace(int value,int newvalue)
{
int times = 0;
for (int i = 0; i < elements; i++)
{
if (a[i] == value)
{
a[i] = newvalue;
times++;
}
}
return times;
}
void input()
{
cout << "How many elements do you want to enter. N<=10";
cin >> elements;
for (int i = 0; i < elements; i++)
{
cout << "enter an element";
cin >> a[i];
}
}
void display()
{
cout << "position value";
cout << "----------------------------------";
for (int i = 0; i < elements; i++)
{
cout << i + 1 << " " << a[i];
}
}
int main()
{
int option = -1;
input();
statistics();
while (true)
{
menu();
cin >> option;
//fill the switch case.
switch (option) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
default:
cout << "Invalid option" << endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.