Why is my program getting terminated when i enter user input? - c++

I was practicing some questions from hacker rank and got stuck in this question called sparse-arrays. (https://www.hackerrank.com/challenges/sparse-arrays/problem) I am a beginner and i am not able to find out the error in my code. Please help. Thank you
I think I am not comparing the strings correctly. I tried using compare function but still it did not work.
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
cin>>n;
string strings[n];
for(int i=0; i<n; i++){
cin>>strings[i];
}
int m;
cin>>m;
string queries[m];
for(int i=0; i<m; i++){
cin>>queries[i];
}
//comparing
for(int i=0; i<m; i++)
{
int count=0;
for(int j=0; j<n; j++)
{
if(queries[i]==strings[j])
count++;
}
cout<<count<<endl;
}
}
The output should be the number of times the strings in (query) that have appeared in the (strings) but my program is getting terminated please help.

for(int j=0; i<n; j++)
should be
for(int j=0; j<n; j++)

Related

Int and Float arrays give wrong results after adding numbers using loops

Doing some simple exercies with C++ and I'm stuck...
When my sum[t] array is declared as integer or float it sometimes outputs at the end some crazy values like for example 4239023 or -3.17802e+30 (despite the fact that I add only small numbers from range <-100; 300>). When I change it for double it works correctly. Why int and float don't work here?
#include <iostream>
using namespace std;
int main()
{
int t=0;
int n=0;
int x=0;
cout<<"Amount of sums: ";
cin>>t;
int sum[t];
for (int i=0; i<t; i++)
{
cout<<"Sum no. "<<i+1<<". How many numbers you wish to add: ";
cin>>n;
for (int j=0; j<n; j++)
{
cout<<"Insert number "<<j+1<<" : ";
cin>>x;
sum[i]+=x;
}
}
for (int i=0; i<t; i++)
{
cout<<sum[i]<<endl;
}
return 0;
}
You should initialize your array of sums to zeroes after you receive the value of t. You could do it like this:
for (int i=0; i<t; i++)
{
sum[i] = 0;
}

Adding values of 2 columns from a matrice and putting the sum in a vector

My first question about CPP in Stackoverflow. I hope i get an answer.
#include <fstream>
using namespace std;
ifstream fin("Matr.dat");
ofstream fout("out.dat");
int a[1000][1000];
int b[10000];
int n, m;
void read()
{
fin>>n>>m;
for(int i=1; i<n; i++)
{
for(int j=1; j<m; j++)
fin>>a[i][j];
}
}
int vect()
{
int c=1;
for(int i=2; i<=n; i++)
{
for(int j=2; j<=m; j++)
{
b[c]=a[j]+a[j+1];
c++;
}
}
return c;
}
int main()
{
read();
int c=vect();
for(int i=0; i<c; i++)
fout<<b[i]<<' ';
return 0;
}
I and a colleague are trying to add the values of 2 columns into a vector.
For the line `b[c]=a[j]+a[j+1]; i receive an error saying that i use 2 incompatible types together. It is not working...
Can someone please help me add the values of 2 columns into a vector?

'' was not declared in this scope

I wrote this program for the college. The user must input 2 numbers (n,m) and the program must make an array with the size [n,m]. Then the user will fill the array and the program must find the maximum number of the columns and print the minimum of them. Then it must find the minimum number of the lines and print the maximum. The compiler says 'n' was not declared in this scope and i don't know why.
Can you help me?
Thank's in advance.
#include <iostream>
using namespace std;
void max (int pinakas[n,m]) {
int i,j,max,pinm[m],min;
for (j=0; j<m; j++){
max=pinakas[0,j];
for (i=0; i<n; i++)
if (pinakas[i,j]>max)
max=pinakas[i,j];
pinm[j]=max;
}
min=pinm[0];
for (j=0; j<m; j++)
if (pinm[j]<min)
min=pinm[j];
cout << min;
}
void min (int pinakas[n,m]) {
int i,j,max,pinm[n],min;
for (i=0; i<n; i++){
min=pinakas[i,0];
for (j=0; j<m; j++)
if (pinakas[i,j]<min)
min=pinakas[i,j];
pinm[i]=min;
}
max=pinm[0];
for (i=0; i<n; i++)
if (pinm[i]>max)
max=pinm[i];
cout << max;
}
int main (){
int n,m,i,j;
cin >> n >> m;
int pin[n,m];
for (i=0; i<n; i++)
for (j=0; j<m; j++)
cin >> pin[i,j];
max(pin[n,m]);
min(pin[n,m]);
return 0;
}
You have tones of errors on your code. I suggest you to give a look at parameters in functions and variable declarations.

Bad Access C++ Error

Can you help me fix a bad access error please?
Here is the code:
#include <iostream>
using namespace std;
int main() {
int t,tr=0;
cin>>t;
while (tr<t) {
int n;
cin>>n;
int distance=n;
int number;
number=n*n;
int spiral[n][n];
for (int i=0;i<n;i++) {
for (int j=0; j<n; j++) {
spiral[i][j]=0;
}
}
for (int i=0; i<n;) {
for (int j=0; j<n;) {
spiral[i][j]=number;
number=number-1;
//cout<<"ij"<<endl;
for (int k=0; k<distance; k++) {
i++;
spiral[i][j]=number;
number--;
//cout<<"k"<<endl;
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout<<spiral[i][j];
}
cout<<endl;
}
tr++;
}
return 0;
}
Bad access is on
spiral[i][j]=number;
Here is the link for the problem but this is not important at the moment. I tried nszmobies but it didn't work so I'm asking you.
This is c++.
Here is the problem.
It seems that you have errors in your loops.
Loop
for (int j=0; j<n;)
looks as it is infinite because j variable isn't changing. Moreover variable i in
spiral[i][j]=number;
in your program can be greater or equal to n.

How do i output sum of rows of a 2D array C++

EDIT: I'm fairly new to c++. Began working with this language two weeks ago.
Sorry if this have been asked before, but i have searched everywhere on the web on how to sum individual rows in a 2D array and not found the answer that i was looking for.
i need to display the sum of each individual row in a[m][n], but for some reason this only works when my array is 2x2, but if it's 3x3 or bigger, then i get the following output in the terminal:
for intsance, a[3][3]=
{1,2,3}, //this is determined by the user
{1,2,3},
{1,2,3};
then i get the following output:
9179942 //address of some sort???
6 // actual sum. code is working here (yay :D)
469090925// again something i dont understand
This is what i have so far
#include <iostream>
using namespace std;
int main(){
int m,n;
cout<<"Enter number of rows for array"<<endl;
cin>>m;
if (m>10){
cout<<"More than 10 rows will be too big"<<endl;
return 1;
}
cout<<"Enter number of collumns for array"<<endl;
cin>>n;
if (n>10){
cout<<"More than 10 collumns will be too big"<<endl;
return 1;
}
int a[m][n];
for(int i=0; i<m;i++){
cout<<"Enter "<<m<<" values into row "<<i+1<<endl;
for(int j=0; j<n; j++){
cout<<"a ["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];
}
}
cout<<"Array dimensions: "<<m<<"x"<<n<<'\n'<<"resulting array: "<<endl;
for(int i=0; i<m;i++){
for(int j=0; j<n; j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
avg[i]+=a[i][j];
}
}cout<<"\n\n";
for(int i=0; i<m; i++){
cout<<avg[i]<<endl;
}
return 0;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
You're not initializing these values so they're free to be whatever cruft is on the stack at the time. You need to initialize them.
int avg[m];
for (int i = 0; i < m; ++i) {
avg[i] = 0;
for (int j = 0; j < n; ++j) {
avg[i] += a[i][j];
}
}
int a[m][n]; is not allowed in Standard C++. The dimensions of C-style arrays must be known at compile-time. A program using this code could do literally anything.
You could replace this line with:
vector<vector<int>> a(m, vector<int>(n));
which seems like a mouthful at first, but you will find it makes your problem go away.
Another bonus of this approach is that you can then use range-based loops:
for(int x : avg)
cout << x << endl;
which reduces the chance of making an error by using the wrong letter in the loop condition.