In C++ Last Modified 02/24/2016 Key Concepts: Arrays, key looping problems again
ID: 3673799 • Letter: I
Question
In C++ Last Modified 02/24/2016 Key Concepts: Arrays, key looping problems again, physical size vs. logical size, programming practices We'll review the three recurring looping scenarios in the context of arrays. For this lab, write your code within the main function; do not use any external methods. Follow the IPO model strictly and clearly label each of the three phases with comments. Here are your tasks: Create an array that could hold up to 25 input numbers, all whole numbers. Let's say the inputs will all be positive. Let's let the user say when it's time to quit. Prompt the user to input numbers using a negative sentinel and save all the inputs in an array. Remember to track your logical size as you go (the counting problem) and remember not to let the user exceed the size of the array. Write a loop to calculate the sum of the elements entered. Write another loop to calculate the sample standard deviation of all of the elements entered. (You might need to look back to Lab 4, and that's okay.) Write another loop to count how many of the inputs are exact multiples of 12. Write another loop to find the smallest value entered. Report all of the statistics at the end of the program. Test your program with various test cases with at least 8 inputs. Change the program so that it could handle 50 inputs instead of 25. This needs to affect two things in the code, but you should make only one change. Here's a sample interaction: Enter a positive integer or -1 to quit: 33 Enter a positive integer or -1 to quit: 77 Enter a positive integer or -1 to quit: 82 Enter a positive integer or -1 to quit: 40 Enter a positive integer or -1 to quit: 1000 Enter a positive integer or -1 to quit: 1 Enter a positive integer or -1 to quit: -1 Sum: 1233 Standard Deviation: 390.375 Number of exact multiples of 12: 0 Smallest: 1
Here's a sample interaction:
Explanation / Answer
The desired solution is as below,
Solution:
#include <iostream>
#include <Math.h>
using namespace std;
int main() {
int nums[25];
int index=0;
//Taking user arguments
while(index < 25)
{
cout <<"Enter a positive number or -1 to quit" << endl;
int numInStr;
cin >> numInStr;
if(numInStr < 0)
{
break;
}
else
{
nums[index++] = numInStr;
}
}
//Calculate Sum
int sum=0;
for(int i=0; i <= index; i++)
{
sum +=nums[i];
}
cout << "Sum :" + sum << endl;
//Calculate Standard daviation
int mean = sum / index;
int sumOfSquare = 0;
for(int j=0; j <= index; j++)
{
sumOfSquare += (nums[j] - mean)*(nums[j] - mean)
}
int meanOfSquare = sumOfSquare/index;
float sd= sqrt(meanOfSquare);
cout << "Standard Deviation :" + sd << endl;
//Number of exact multiple of 12
int count=0;
for(int i=0; i <= index; i++)
{
if(nums[i] %12 == 0)
{
count++;
}
}
cout << "Number of exact multiple of 12:" + count << endl;
//Smallest
int smallest= nums[0];
for(int i=1; i <= index; i++)
{
if(nums[i] < smallest)
{
smallest = nums[i];
}
}
cout << "Smallest" + smallest << endl;
return 0;
}