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

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

Related

c++ shorter way to display the next 5 numbers by given inputs?

i've just learned cpp and want to know is there a shorter way to display a sequential number.
this is my code
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Input number: ";
cin>>n;
cout<<"The next 5 rows of number is :"<<n+1<<n+2<<n+3<<n+4<<n+5;
}
A simple loop should solve your problem:
int main(){
int n;
cout << "Input number: ";
cin >> n;
cout << "The next 5 rows of number are: ";
for (int i = n + 1; i <= n + 5; i++) {
cout << i << ' ';
}
}

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;
}

Sum of long integer array in c++

I know this question asked many times but I am facing different problem in my code, I try to calculate sum of long integers range between 2-15.
Code:
long array[20];
long NUMBERS;
cout << "How many numbers ? : ";
cin >> NUMBERS;
long sum=0;
for (int i = 0; i < NUMBERS;i++){
cout << "Input number " << (i+1) << " : ";
cin >> array[i];
}
cout << "Calculate Sum" << endl;
for (int i = 0; i < NUMBERS;i++){
sum = sum + array[i];
}
cout << "Sum is : " << sum << endl;
When I input these three numbers.
1234567
123456
12345
Output:
Sum is : 1370368
but actual answer is : 3703627.
I try these solutions
summing-large-numbers and sum-of-alternate-elements-of-integer-array but still not get right solution, also how we can solve this problem if user input different number with different ranges.
This isn't about programming, but math...
Hope this helps: http://www.wikihow.com/Add-Large-Numbers
(As a simple example, add 1 and 11. What is the result? 12 or 21?)
my code sum large number with string.
first you enter number of numbers you want to sum(to 25).
and then you enter the number (to 180 'each number').
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (){
int band;
cin >> band;
string string_of_number[25];
for (int i=0; i<band; i++){ //for get all string number
cin >> string_of_number[i];
}
int strings_length[band];
for (int i=0; i<band; i++){ //for get all length of strings
strings_length[i]=string_of_number[i].length();
}
int answer[180];
for(int i=0; i<180; i++){
answer[i]=0;
}
int remaner=0;
int sum=0;
int last=180;
for (int h=0; h<band; h++){ // for sum strings with sum answer one by one
int j=179;
for (int i=strings_length[h]; i>=0; i--){
if(string_of_number[h][i]=='\0'){
i--;
}
sum = (remaner + (string_of_number[h][i]-'0') + answer[j]) %10;
remaner = ((string_of_number[h][i]-'0') + answer[j]) / 10;
answer[j]=sum;
if (i==0 && remaner>0){
j--;
answer[j]+=remaner;
remaner=0;
}
if(last>j)
last=j;
j--;
}
}
for(; last<180; last++){
cout << answer[last];
}
}
It seems that your program assumes all numbers are 7 digit:
1234567
123456[0]
12345[00]

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 to most efficiently take in 10 different numbers from a user, add them to an array, and then output all of those numbers?

New to C++, I'm trying to make a program that takes in 10 different numbers, and it takes all of those numbers and adds them to an array list which prints them out at the end.
My code: (that doesn't work)
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
int numbers[10];
int input;
cout << "Please enter ten numbers" << endl;
for(int i=0;i<10;i++){
cin >> input;
int numbers[i] = numbers + input;
}
cout << numbers;
}
When I run this and put in all 1's for all ten numbers, I expect to get an output like 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
but instead I get 0x7d88afc45630 which is weird and I'm not really sure why. I think it has to do with how I'm adding the numbers into the array list, which I'm not even sure how to do. I'd also like to be able to return the largest and smallest values of the list, but I need to figure this out first.
int numbers[i] = numbers + input;
It's converting the address the memory for numbers is at to an integer and adding it to input.
Do you just mean
numbers[i] = input;
On top of that, cout << numbers just prints out the memory location of numbers.
You probably want something like:
for (int i = 0; i < 10; i++) {
cout << numbers[i] << endl;
}
Changed the output variable used as you don't need an array for this. Also, you don't need to put the data type before the variable name when assigning a value to it if you already declared that variable. See the modified code below:
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
int sum = 0;
int input;
cout << "Please enter ten numbers" << endl;
for(int i=0;i<10;i++)
{
cin >> input;
sum += input;
}
cout << sum;
}
If what you mean by "adding to an array" is putting the user input values to an array, just change these parts
int sum = 0;
:
sum += input;
:
cout << sum;
to
int numbers[10];
:
numbers[i] = input;
:
for(int i=0;i<10;i++)
{
cout << numbers[i] << endl;
}