Home > front end >  Subtract each elements of an array consecutively
Subtract each elements of an array consecutively

Time:11-14

I have an array and I want to subtract each of the elements consecutively, ex: {1,2,3,4,5}, and it will result to -13 which is by 1-2-3-4-5.

But I don't declare or make those numbers fixed as they're taken from the input (user). I only make it like, int array[100] to declare the size.

Then, to get the inputs, I use the for loop and insert them to the array. Let's say first input is 10, then array[0] must be 10 and so on.

The problem is, how do I subtract them? I have two options:

  1. The first element of the array (array[0]) will subtract the next element (array[1]) right after the user input the second element, and the result (let's say it's int x) will subtract the next element (array[2]) after the user input it and so on.

  2. I'll have the user input all the numbers first, then subtract them one by one automatically using a loop (or any idea?) *These elements thing refer to the numbers the user input.

Question: How do you solve this problem?

(This program will let the user input for as much as they want until they type count. Frankly speaking, yeah I know it's quite absurd to see one typing words in the middle of inputting numbers, but in this case, just how can you do it?)

Thanks.

Let's see my code below of how I insert the user input into the array.

string input[100];
int arrayInput[100];
int x = 0;
for (int i = 0; i >= 0; i  ) //which this will run until the user input 'count'
{
   cout << "Number " << i 1 << ": ";
   cin >> input[i];
   arrayInput[i] = atoi(input[i].c_str());
   ...
   //code to subtract them, and final answer will be in int x
   ...
   if (input[i] == "count")
   {
      cout << "Result: " << x << endl;
   }
}

CodePudding user response:

You can/should use a dynamic sized container like std::vector as shown below:

#include <iostream>
#include <vector>

int main()
{
   
    int n = 0;
    
    //ask user how many input he/she wants to give 
    std::cout << "How many elements do you want to enter: ";
    std::cin >> n;
    
      std::vector<int> vec(n); //create a vector of size n 
      
      int resultOfSubtraction = 0;
      //take input from user 
      for(int i = 0 ; i < n ;   i)
      {
          std::cin >> vec.at(i);
          
            if(i != 0)
            {
                resultOfSubtraction-= vec.at(i);
            }
            else 
            {
              resultOfSubtraction = vec.at(i);  
            }
      }
      std::cout<<"result is: "<<resultOfSubtraction<<std::endl;
    return 0;
}

Execute the program here.

If you want a string to end the loop then you can use:

#include <iostream>
#include <vector>
#include <sstream>
int main()
{
    
    std::vector<int> vec;

    int resultOfSubtraction = 0, i = 0;
    std::string endLoopString = "count";
    
    std::string inputString;
    int number = 0;
      //take input from user 
    while((std::getline(std::cin, inputString)) && (inputString!=endLoopString))
    {
        std::istringstream ss(inputString);
        if(ss >> number)
        {
            vec.push_back(number);
            if(i == 0)
            {
                resultOfSubtraction = number;
            }
            else 
            {
                resultOfSubtraction-= number;
            }
              i;
        }
        
    }
   std::cout<<"result is: "<<resultOfSubtraction<<std::endl;
return 0;
}
  •  Tags:  
  • c
  • Related