A problem of ending the implementation of the code - c++

this code works well for all samples, but after all samples are finished, a problem occurs. I don’t know what happens and the program crashes. Is there a problem with this code?
i have this problem when i use strings arrays usualy can it be the problem?
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
long long t,n;
int a[1000];
string str[1000];
int main()
{
cin>>t;
for(int r=1;r<=t;r++){
cin>>n;
int maxi=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]>maxi)maxi=a[i];
};
//input first value
maxi=maxi+3;
for(int r1=0;r1<maxi;r1++){
str[1][r1]=(rand()%26)+'a';
}
for(int i=0;i<maxi;i++){
cout<<str[1][i];
}
cout<<endl;
//
for(int k=2;k<=(n+1);k++){
int w;
for(w=0 ; w<=a[k-1];w++){
str[k][w]=str[k-1][w];
};
for(int l=w-1;l<maxi;l++){
str[k][l]=(rand()%26)+'a';
};
for(int i=0;i<maxi;i++){
cout<<str[k][i];
}
cout<<endl;
}
}
return 0;
}

You are using elements of strings without allocating them.
Allocate elements by inserting
for(int i=1;i<=(n+1);i++){
str[i].resize(maxi);
}
just after
maxi=maxi+3;

Related

Input different values into set without using extra variable (C++)

I want to store values into set without using extra variable.
int n,x;
set<int> s;
cin>>n;
for(int i=0;i<n;i++)
{
cin >> x;
s.insert(x);
}
This is what i have been doing till now, was wondering if i could remove x, as this variable is useless.
I am new to C++. Forgive me if my doubt is very naive.
was wondering if i could remove x
Sure.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
int main() {
std::set<int> s;
int n;
std::cin >> n;
std::copy_n(std::istream_iterator<int>{std::cin}, n,
std::inserter(s, s.begin()));
}

c++ Scrabble game using arrays

I have to make a scrabble scoring game using 2 arrays. The first array holds the user inputted word and the second array holds the value of each letter. Lastly a function is needed to calculate the score. Im having trouble assigning the user values to the second array to get the score and getting the right code for the function.
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
int scoreCalculator(int total);
char userWord;
int points;
int total=0;
int main()
{
char userWord[11];
for (int i=0; i<11; i++)
{
userWord[i]='\0';
}
cout<<"Enter your word less than 10 letters: "<<endl;
cin>>userWord;
cout<<"Here is the word you inputted: "<<userWord<<endl;
int scrabblePoints[26]={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
for(int j=0; j<26; j++)
{
userWord[i]
}
cout<<"Here is your score: "<<scoreCalculator(total)<<endl;
}
int scoreCalculator(int total)
{
total+=scrabblePoints[j];
}
This is what I have so far and where im stuck at
#include <iostream>
int main()
{
std::string input;
std::cin>>input;
// fill this with scrable values
int scrable_values[26] = {1,3,3,2,1,4,2,4,1,9,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
int ans = 0;
for(int i=0;i<input.size();i++)
{
ans += scrable_values[input[i]-97];
}
return ans;
}

matrix rotation segmentation fault

Writing code for the matrix rotation question in hackerrank. i am getting segmentation fault. the code is not complete. i have commented on the statements which i think is creating the prblem but cant identify the mistake . plz help.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct coord
{
int i,j;
} starting, current;
struct reference
{
float i,j;
} center;
int **a,m,n,t1,t2;
int goup()
{
float dist,dist2;
dist=sqrt((center.i-current.i)*(center.i-current.i)+(center.j-current.j)*(center.j-current.j));
do
{
t1=a[current.i-1][current.j];
a[current.i-1][current.j]=t2;//segmentation fault at this statement
t2=t1;
current.i--;
dist=sqrt((center.i-current.i)*(center.i-current.i)+(center.j-current.j)*(center.j-current.j));
} while(dist2!=dist);
return 0;
}
int main()
{
int t;
cin>>m>>n>>t;
a=new int*[m];
for(int i=0;i<m;i++)
a[i]=new int[n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
center.i=(float)(m-1)/2.0;
center.j=(float)(n-1)/2.0;
starting.i = center.i + (0.5)*(m%2+1);
starting.j = center.j + (0.5)*(n%2+1);
current.i=starting.i;
current.j=starting.j;
t2=a[starting.i][starting.j];
t1=a[starting.i-1][starting.j];
while(current.i!=m)
{
for(int i=0;i<t;i++)
{
goup();
// goleft();
// godowm();
// goright();
}
starting.i++;
starting.j++;
current.i=starting.i;
current.j=starting.j;
}
return 0;
}
Since dist2 is never initialised, dist2!=dist is rarely true and you decrement current.i so many times that you wind up accessing outside the array and dereferencing whatever random number might be lying around there.
It's impossible to say how to fix it since you haven't left any clue about what the function is supposed to accomplish.
Initialising dist2 with whatever value you want it to have would be a good start, but the condition is still unlikely to ever be true.

why error when i have big input

if n is for example 1.000.000 compiler stops working. Why? Long int dont help.
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
ifstream fin;
fin.open("share.in");
fout.open("share.out");
int n;
fin>>n;
int array1[n];
for(int i=0; i<n; i++)
{
fin >> array1[i];
}
int suma=0, sumb=0, sumc=0, a=1, b=1, c=n-2, a1=0, b1=0, c1=0, apotelesma=1000000000, mikroterhdiafora=1000000000;
while(c>0)
{
while(a1<a)
{
suma+=array1[a1];
a1++;
}
while(b1<b)
{
sumb+=array1[(a1+b1)];
b1++;
}
while(c1<c)
{
sumc+=array1[(a1+b1+c1)];
c1++;
}
if(max(abs(suma-sumb),max(abs(sumb-sumc),abs(sumc-suma)))<=mikroterhdiafora)
{
mikroterhdiafora=min(mikroterhdiafora, max(abs(suma-sumb),max(abs(sumb-sumc),abs(sumc-suma))));
apotelesma=min(apotelesma, max(suma,max(sumb,sumc)));
}
suma=0;
sumb=0;
sumc=0;
a1=0;
b1=0;
c1=0;
c--;
b++;
if(c==0)
{
++a;
b=1;
c=n-a-1;
}
}
fout<<apotelesma;
fin.close();
fout.close();
return 0;
}
1,000,000 is a bit too much for a static array. You should create it dynamically:
int* array1 = new int[1000000];
// Do operations on array...
delete[] array1;
The cause of this behaviour is that when you create static array (int array1[1000000]), compiler puts it on the stack, which has limited capacity and when you exceed that limit, you have unfamous stack overflow, which leads to your problem. Dynamic memory on the other hand is stored on the heap and is limited only by your hardware, but unlike in the stack, you have to remember to free it, when it's not longer needed.

Why my code is giving wrong answer on SPOJ?

Problem: http://www.spoj.com/problems/TSORT/
Below code is giving correct output on my computer but it is giving wrong answer on spoj. I have tried this with several inputs and it is giving correct output. But still showing wrong answer on spoj.
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
cin.tie(0);
ios::sync_with_stdio(false);
int *arr= new int[t];
for(int i=0;i<t;i++)
{
cin>>arr[i];
}
sort(arr,arr+t);
for(int i=0;i<t;i++)
{
cout<<"\n";
cout<<arr[i];
}
return 0;
}
As Tejas says, using cin and cout is not recommended in problems of optimization, as they are slower in comparison to scanf and printf. Extending his answer, you need to note that you use the STL sort that is slower than STL qsort. having this in mind, I tried your code with qsort and i got AC:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
int a[1000008];
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main()
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
qsort(a,n,sizeof(int),compare);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
The int function compare is needed for the usage of qsort.
More info:
http://www.cplusplus.com/reference/cstdlib/qsort/
http://www.cplusplus.com/reference/algorithm/sort/
If you use cout<<arr[i]<<endl; it will give TLE instead of WA. So it means it has something to do with new line.
However if you want to get your answer accepted, you have to use printf and scanf instead of cin, cout. It is always suggested in such contests to use scanf and printf for reading large inputs.
I tried the following code and it was accepted
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1000008];
int main()
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}