Home > Back-end >  C How to read input N and then read a series of numbers N long?
C How to read input N and then read a series of numbers N long?

Time:11-25

I'm working on an assignment where I need to create a program that reads a non-empty sequence of integer numbers, and tells how many of them are equal to the last one.

It should read the amount of integers the sequence has and then read the sequence itself and return the amount of times the last number repeats itself excluding the last one.

Input would be like this:

9
1 7 3 2 4 7 5 8 7

and the output should be:

2

Now, I have no problem with the functionality of the program but I'm struggling on having it read the inputs. This is what I got:

#include <iostream>
#include <vector>
#include <sstream>

int NumbersEqualToLast(int limit, std::vector<int> elements) {
  int check = elements[limit], counter = 0;
  for (int i = limit; i >= 0; i--) {
    if (elements[i] == check) {
      counter  ;
    }
  }
  return(counter);
}

int main() {
  int amount, number;
  std::cin >> amount;

  std::string input;
  getline(std::cin, input);
  std::stringstream iss(input);
  std::vector<int> numbers;
  while ( iss >> number ) {
    numbers.push_back( number );
  }
  std::cout << NumbersEqualToLast(amount, numbers) << std::endl;
} 

The problem is after reading the amount of integers (in this case 9) I get a Segmentation fault (core dumped) error.

EDIT AFTER SOLUTION

This is what worked for me using your suggestions. Thank you. I understand if it's not pretty or there are better more efficient ways but I'm just starting out. :)

#include <iostream>
#include <vector>

int NumbersEqualToLast(int limit, std::vector<int> elements) {
  int check = elements[limit - 1], counter = 0;
  for (int i = limit - 2; i >= 0; i--) {
    if (elements[i] == check) {
      counter  ;
    }
  }
  return(counter);
}

int main() {
  int quantity;
  std::cin >> quantity;
  int number;
  std::vector<int> numbers;
  for (int i = 0; i < quantity; i  ) {
    std::cin>>number;
    numbers.push_back(number);
  }
  std::cout << NumbersEqualToLast(quantity, numbers) << std::endl;
}

CodePudding user response:

Just don't judge harshly, please. I suggest another way to solve this problem.

#include <iostream>
#include <vector>
using namespace std;
int special, count;
void dfs(int current, int previous, vector<int>& visited, vector<int>& input)
{
    if(visited[current]==1)
    {
        return;
    }
    visited[current]=1;
    if(current==(input.size()-1))
    {
        special=input[current];
    }
    for(int next=(current 1); next<input.size();   next)
    {
        if(next==previous)
        {
            continue;
        }
        dfs(next, current, visited, input);
    }
    if(special==input[current])
    {
          count;
    }
    if(current==0)
    {
        --count;
        cout<<count;
    }
    return;
}
void solve()
{
    int quantity;
    cin>>quantity;
    int number;
    vector<int> numbers;
    for(int i=0; i<quantity;   i)
    {
        cin>>number;
        numbers.push_back(number);
    }
    vector<int> visited(quantity);
    dfs(0, -1, visited, numbers);
    return;
}
int main()
{
    solve();
    return 0;
}

Input:

9
1 7 3 2 4 7 5 8 7

Here is the result:

2
  • Related