Please help edit the following program so that the user can input their own arra
ID: 3606273 • Letter: P
Question
Please help edit the following program so that the user can input their own array of numbers that are then searched for particular number indexes.
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
void main()
{
int RandomData[] = { 9,5,23,44,21,68,26,33,26,72 };
int infant = 0, young = 0, middle = 0, old = 0, veryold = 0;
int n, freq, count = 0, high = 0;
double stdDev, mean, sum = 0;
for (int i = 0;i<(sizeof(RandomData) / sizeof(RandomData[0]));i++)
{
if (RandomData[i]>0 && RandomData[i] <= 18)
infant++;
else if (RandomData[i]>18 && RandomData[i] <= 29)
young++;
else if (RandomData[i]>29 && RandomData[i] <= 50)
middle++;
else if (RandomData[i]>50 && RandomData[i] <= 69)
old++;
else if (RandomData[i]>69)
veryold++;
}
double first = 0, second = 0;
for (int i = 0;i<(sizeof(RandomData) / sizeof(RandomData[0]));i++)
{
n = RandomData[i];
count = 0;
for (int j = 0;j<(sizeof(RandomData) / sizeof(RandomData[0]));j++)
{
if (n == RandomData[j])
count++;
}
if (high<count)
{
freq = RandomData[i];
high = count;
}
first += (pow((RandomData[i]), 2) / (sizeof(RandomData) / sizeof(RandomData[0])));
second += RandomData[i] / (sizeof(RandomData) / sizeof(RandomData[0]));
sum += RandomData[i];
}
mean = sum / (sizeof(RandomData) / sizeof(RandomData[0]));
stdDev = sqrt(first - pow((second), 2));
cout << "Number in age range infant: " << infant << endl;
cout << "Number in age range young: " << young << endl;
cout << "Number in age range middle: " << middle << endl;
cout << "Number in age range old: " << old << endl;
cout << "Number in age range older: " << veryold << endl << endl;
cout << "Mean: " << mean << endl;
cout << "Mode: " << freq << endl;
cout << "Standard Deviation: " << stdDev;
getch();
}
Explanation / Answer
Hi, I have added code to take user input.
Please let me know in case of any issue.
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<cmath>
#include <cstdlib>
using namespace std;
void main()
{
int n, num;
cout<<"How many numbers you want to enter: ";
cin>>n;
int *RandomData = new int[n]; // creating array
cout<<"Enter "<<n<<" numbers: "<<endl;
for(int i=0; i<n; i++)
cin>>RandomData[i];
int infant = 0, young = 0, middle = 0, old = 0, veryold = 0;
int n, freq, count = 0, high = 0;
double stdDev, mean, sum = 0;
for (int i = 0;i<(sizeof(RandomData) / sizeof(RandomData[0]));i++)
{
if (RandomData[i]>0 && RandomData[i] <= 18)
infant++;
else if (RandomData[i]>18 && RandomData[i] <= 29)
young++;
else if (RandomData[i]>29 && RandomData[i] <= 50)
middle++;
else if (RandomData[i]>50 && RandomData[i] <= 69)
old++;
else if (RandomData[i]>69)
veryold++;
}
double first = 0, second = 0;
for (int i = 0;i<(sizeof(RandomData) / sizeof(RandomData[0]));i++)
{
n = RandomData[i];
count = 0;
for (int j = 0;j<(sizeof(RandomData) / sizeof(RandomData[0]));j++)
{
if (n == RandomData[j])
count++;
}
if (high<count)
{
freq = RandomData[i];
high = count;
}
first += (pow((RandomData[i]), 2) / (sizeof(RandomData) / sizeof(RandomData[0])));
second += RandomData[i] / (sizeof(RandomData) / sizeof(RandomData[0]));
sum += RandomData[i];
}
mean = sum / (sizeof(RandomData) / sizeof(RandomData[0]));
stdDev = sqrt(first - pow((second), 2));
cout << "Number in age range infant: " << infant << endl;
cout << "Number in age range young: " << young << endl;
cout << "Number in age range middle: " << middle << endl;
cout << "Number in age range old: " << old << endl;
cout << "Number in age range older: " << veryold << endl << endl;
cout << "Mean: " << mean << endl;
cout << "Mode: " << freq << endl;
cout << "Standard Deviation: " << stdDev;
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.