How would I make the program receive user input? - c++

Ok, so, on my last question my program was finding the standard deviation of a set of numbers. Today, my instructor told me that it needs to get multiple numbers from the user. I have no clue on how about going to do this. Any advice on how to do this? Code is accepted. Please and thanks.
#include "stdafx.h" //No Flipping Clue
#include <iostream> //Needed For "cout"
#include <iomanip> //Needed To Round Decimal Points
#include <math.h> //Needed For "sqrt()" And "pow()"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Used To Round The Decimal Points 2 Places
cout << setiosflags(ios::fixed|ios::showpoint);
cout << setprecision(1);
//Declaring
double Numbers[] = {65, 49, 74, 59, 48}; //Work On Making This A User Input
double Mean = 0, Items = 0, Sum = 0, Deviation = 0;
int Counter;
//Finds The Mean Of The Set Of Numbers
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
Sum += Numbers[Counter]; //Adds All Numbers In Array Together
}
Items = sizeof(Numbers) / sizeof(double); //Gets The Number Of Items In The Array
Mean = Sum / Items; //Finds The Mean
}
//Finds The Standard Deviation
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
Deviation += pow((Numbers[Counter] - Mean), 2) / Items; //Does Math Things...
}
Deviation = sqrt(Deviation); //Square Roots The Final Product
cout << "Deviation = " << Deviation << endl; //Print Out The Standard Deviation
system("pause");
return 0;
}

A quick google search would have done the job ... C++ Input/Ouput
int number;
cin >> number;
Exemple:
int nbNumbers;
cout << "How many numbers do you need :" << endl;
cin >> nbNumbers;
double numbers[nbNumbers];
for(int i = 0; i < nbNumbers; ++i)
{
cout << "Enter a Number :" << endl;
cin >> numbers[i];
}

there you go:
cout<<"how many numbers you want to enter?";
cin>>n;
double Numbers[n];
cout<<"enter numbers";
for(int i=0;i<n;i++){
cin>>Numbers[i];
}

Here is an example of vector. It takes input from user until user enters -1.
#include <stdio.h>
#include <array>
int main()
{
int number;
vector<int> userInput;
do
{
cout << "Enter a number (-1 to quit): ";
cin >> number;
userInput.push_back(number);
} while (number != -1);
for (vector < int >::iterator it = userInput.begin(); it < userInput.end() - 1; it++)
{
cout << endl << *it;
}
system("pause");
return 0;
}

Related

How to add some numbers inputed by the user given a specific formula

So i need to write a program that asks for some numbers from the user (the amount of numbers is determined by the user) and then add them given this formula: ANSWER = FIRST - SECOND + THIRD - FIFTH + ...
where FIRST, SECOND, etc are the first, second and the rest of the numbers input by the user.
The problem is that i can create a loop that stores the numbers but actually, it only updates the value of the "num" variable. This is the code i have written.
#include <iostream>
using namespace std;
int main() {
int num, counter;
double answer;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
}
return 0;
}
Inserting a if-else clause that controls the remainder of the integer division of index i by 2 you can separate even and odd cases to obtain the desidered effect
#include <iostream>
using namespace std;
int main() {
int num, counter;
double answer;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
if(i%2==0)
answer+=num;
else
answer-=num;
}
return 0;
}
You can also do this assuming you don't need to store the numbers input by the user. What I am basically doing is just toggling between +1 and -1 that I then multiply by the number input by the user and then straightforwardly adding it to the answer.
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int num, counter;
double answer = 0;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
answer += num*pow(-1, i);
}
cout<<answer;
return 0;
}
You can also do:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int num, counter;
double answer = 0;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
if(i%2 == 0)answer += num;
else answer -= num;
}
cout<<answer;
return 0;
}

How to record multiple values based upon a user-entered number?

My lab professor wants us to make a program that "Use a for loop to write a program that calculates the product of x integers entered by the user; where x is entered by the user as well. Repeat the question using while or do-while."
After further clarification, I realized he meant something like the user enters a number, say 5. The program will then prompt the user to enter 5 numbers. The program then displays the product of those 5 entered numbers. I get how it works now, but i don't understand how I can store all those numbers and bring them out for multiplication later.
This is all I have so far:
#include <iostream>
using namespace std;
int main() {
int numofNumbers = 0;
cout << "Enter the number of numbers you want multipled: ";
cin >> numofNumbers;
for (numofNumbers; numofNumbers > 0; numofNumbers = numofNumbers - 1) {
cout << "Enter a number"; //how can I record these values then multiply them?
cout << endl;
}
system("pause");
return 0;
}
Complete code using WHILE loop
#include <iostream>
using namespace std;
int main()
{
int n, count = 0;
int input, result = 1;
cout<<"Enter number: ";
cin>>n; // n numbers
while (count < n)
{
cout<<"Enter number "<<count + 1<<": ";
cin>>input; // taking input
result *= input; // multiplying
count++;
}
cout<<"Total result is: "<<result;
return 0;
}
You're assuming that you need to store all the user-provided numbers in order to multiply them - this is not true. I suggest spending more time thinking on a way to solve your assignment without storing all the passed numbers.
In order to answer your question: the most common way of storing items in C++ is using std::vector. In order to understand what it is doing, you should have some knowledge about arrays and carefully read the documentation.
So you managed to calculate the factor of those numbers, now you want to store them to use it later.
You can use a the vector container from the STL to store your numbers. Bellow is an example :
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers;
int numofNumbers = 0;
int number;
cout << "Enter the number of numbers you want multipled: ";
cin >> numofNumbers;
for (numofNumbers; numofNumbers > 0; numofNumbers = numofNumbers - 1) {
cout << "Enter a number";
cin >> number;
numbers.push_back(number); // store the numbers
cout << endl;
}
// do what you want with stored numbers in the vector....
// ....
//
for (auto it: numbers){
cout << it << std::endl;
}
return 0;
}
If storing N number isn't a requirement, you can multiply values on place:
int numofNumbers = 0;
int result = 1
cout << "Enter the number of numbers you want multipled: ";
cin >> numofNumbers;
for (; numofNumbers > 0; numofNumbers = numofNumbers - 1) {
cout << "Enter a number";
int num = 1;
cin >> num;
result *= num;
}
if (numofNumbers > 0) {
cout << "Multiplication result is: " << result;
}
You don't need to store numbers. Just multiply as a number is entered.
#include <iostream>
using namespace std;
int main() {
unsigned int numofNumbers = 0;
int product = 0;
int number = 0;
int tempNumofNumbers = 0;
cout << "Enter the number of numbers you want multipled: ";
cin >> numofNumbers;
tempNumofNumbers = numofNumbers;
while(tempNumofNumbers) {
cout << "Enter a number";
cin >> number;
if(tempNumofNumbers == numofNumbers)
{
product = number;
}
else
{
product *= number;
}
tempNumofNumbers--;
}
cout << "product" << product;
system("pause");
return 0;
}
For recording input, you could use:
#include <iostream>
int main() {
int numofNumbers = 0;
std::cout << "Enter the number of numbers you want multipled: ";
std::cin >> numofNumbers;
double total = 1;
for (int counter = numofNumbers; counter > 0; counter--)
std::cout << "Enter a number: " << std::endl;
std::cin >> input_number;
total = total * input_number; //multiple the numbers
}
if (numofNumbers > 0) {
std::cout << "total is: " << total << std::endl;
}
system("pause");
return 0;
}

How can I find a missing number in a sequence of numbers?

Numbers should be input as:
53,55,57,58,54
Output:
Missing number is 56
Input should be able to be as many numbers as wanted.
This is what I have so far:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int getMissingNo (int a[], int n)
{
int i, total;
total = (n+1)*(n+2)/2;
for ( i = 0; i< n; i++)
total -= a[i];
return total;
}
int main()
{
int a[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
cout << "Enter number of numbers in sequence: ";
int numInSeq;
cin >> numInSeq;
cout << endl;
cout << "Enter numbers in sequence: ";
for (int i = 0; i < numInSeq; i++)
{
cin >> a[i];
}
cout << endl;
int miss = getMissingNo(a,numInSeq);
cout << "Missing number: " << miss << endl << endl;
return 0;
}
The only thing I am missing is being able to enter the numbers separated by commas and I need to edit getMissingNo so it can be any sequence of numbers not just one that starts with 1.
Simple solution if you know the range.
For a given range, let S be the sum of all the numbers within that range.
For a given array with a missing number, let MS be the sum of the numbers in this array.
The missing number is S - MS

How to display all abundant numbers less than a given number

I am having trouble with translating my algorithm into c++ code that will display all abundant numbers less than the inputted number. I want to create a program that will display all abundant numbers less than an inputted number. For example if I entered a number like 19, the console should print out 12 and 18. The algorithm is this.
Scan through each number
Check whether its an abundant number
If it is an abundant number, then print it out
I am using Microsoft Visual Studios 2013 IDE and a compiler that supports c++11.
This is the code I have so far
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number" << endl;
cin >> number;
for (int i = 1; i < number; i++)
{
// Don't know what to put here
}
char inputCharacter;
cin >> inputCharacter;
return 0;
}
I didn't knew, what abundant numbers are, but if you mean this: https://en.wikipedia.org/wiki/Abundant_number
Then this may help you:
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number" << endl;
cin >> number;
for (int i = 1; i < number; i++)
{
int sum = 0;
for(int k = 1; k < i; k++)
{
if(i % k == 0)
{
sum += k;
}
}
if(sum > i)
{
cout << i << endl;
}
}
char inputCharacter;
cin >> inputCharacter;
return 0;
}
If you need any explanation about the algorithm, just ask for it.
int number;
cout << "Enter a number" << endl;
cin >> number;
for (int i ==1; i<number; i++)
{
int temp =0;
for (int j ==1; j<i; j++)
{
if (i%j==0)
{
temp+=j;
}
}
if (temp>i)
{
cout<<j;
}
}
this uses modular division and will accumulate all numbers which are factors.
if the above returns 1 its abundant i think... if i got the right idea of what abundancy is.

2D ArrayLoops C++

Having trouble getting my code to run properly, first time I have ever used C++ and just trying to learn it for my knowledge, I am trying to get a 2d array with all zeros except in the final column. Inputs are stock = 100, strike = 100, time to maturity = 1, interest rate = 0.06, time steps = 3, upfactor = 1.1, downfactor = 0.9091. The end Array should look like {[0,0,0,133.10], [0,0,0,110], [0,0,0,90.91], [0,0,0,75.13]}, bot for some reason I keep getting values in the first column as well and I am stumped. Any advice?
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
int main(int nNumberofArgs, char*pszArgs[])
{
double st;
cout << " Enter Value of stock: ";
cin >> st;
double K;
cout << " Enter Value of strike price: ";
cin >> K;
double t;
cout << " Enter time of maturity: ";
cin >> t;
double r;
cout << " Enter Value of the interest rate: ";
cin >> r;
int N;
cout << " Enter Value of time steps: ";
cin >> N;
double u;
cout << " Enter value of up factor: ";
cin >> u;
double d;
cout << " Enter Value of down factor: ";
cin >> d;
double dt;
dt = t/N;
double p;
p = (exp(r*dt)-d)/(u-d);
// Initialise asset price at maturity time step N
double price[N][N];
for( int i = 0; i < N+1; i++)
{
for (int j = 0; j<N+1; j++)
{
price[i][j] = 0;
}
}
price[N][N] = st*pow(d,N);
cout << "price[N][N] is equal to: " << price[N][N] << endl;
double newN;
newN = N-1;
//cout << price[2][0] << endl;
for(int ii = newN; ii >=0; ii--)
{
price[ii][N] = (price[ii+1][N]) * (u/d);
}
//cout << price[2][0] << endl;
for( int i = 0; i <= N; i++)
{
for (int j = 0; j <=N; j++)
{
cout << price[i][j] << " ";
}
cout << endl;
}
system("PAUSE");
return 0;
}
The problem area is
for(int ii = newN; ii >=0; ii--)
{
price[ii][N] = (price[ii+1][N]) * (u/d);
}
and not sure exactly how to fix it. Any thoughts??
In C/C++ indexes are from 0
double price[N][N];
or
double price[10][10];
means that you have an array from 0..9 and 0..9
so
price[N][N] = st*pow(d,N);
is writing to a location outside the arrays as the maximum index is price[N-1][N-1]
and for that reason, loops in C/C++
for( int i = 0; i <= N; i++)
should be written as
for( int i = 0; i < N; i++)
since N is not included as a valid index value for the array.
Couple of issues with your program.
You have created a double dimensional array on stack with variable sized length (N).
If your array size is dynamic don't create it on stack, use new to allocate it on heap.
Also, as I see it you are accessing out-of-array entries. (Index greater than max array index)