Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

change this code to c++ with

+2 votes
asked Dec 11, 2022 by (160 points)
n,x = map(int,input().split()) lst = list(map(int,input().split())) su = 0 for i in range(n-1) :      z = lst[i] + lst[i+1]     if (z == x ) :          su = su + 1 print(su)

1 Answer

+2 votes
answered Dec 12, 2022 by Peter Minarik (101,360 points)

First of all, you should explain what your program does, as it is not clear...

Your program looks at a list of numbers (lst) and checks if any two neighbors add up to a certain value (x). 

Here's a similar program in C++:

#include <iostream>
#include <vector>

int main()
{
    std::cout << "The program asks for a sequence of integral numbers and checks if any two neighbours sum to a specific value." << std::endl;
    std::cout << "What sum should we look for? : ";
    int targetSum;
    if (!(std::cin >> targetSum))
    {
        std::cerr << "Not a number." << std::endl;
        return -1;
    }
    
    int number;
    std::vector<int> numbers;
    bool numberRead = true;
    std::cout << "Please, enter the next element in the sequence (not a number to finish)." << std::endl;
    do
    {
        std::cout << ": ";
        if (std::cin >> number)
            numbers.push_back(number);
        else
            numberRead = false;
    } while (numberRead);
    
    int matchCount = 0;
    int lastIndex = numbers.size() - 1;
    for (int i = 0; i < lastIndex; i++)
    {
        if (numbers[i] + numbers[i + 1] == targetSum)
            matchCount++;
    }
    
    std::cout << "Number of pairs summing up to " << targetSum << ": " << matchCount << std::endl;

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...