I used CodeBlocks to code this program:
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
ifstream is;
is.open("game.inp");
int n,a[100];
is>>n;
for (int i=0; i<n; i++)
is>>a[i];
is.close();
int game[100];
int kt=0;
for (int i=0; i<n; i++)
{
for (int j=3; j<a[i]+1; j++)
{
if ((a[i]%j)==0)
{
int *x = find(begin(game),end(game),j); //ktra uoc hien tai da co trong mang hay chua, k co thi ms them
if (x==end(game))
{
game[kt]=j;
kt++;
}
}
}
}
int kq=0;
for (int i=0; i<kt; i++)
{
int d=0;
for (int j=0; j<n; j++)
{
if ((a[j]%game[i])==0)
d++;
}
if (d>kq)
kq=d;
}
ofstream o;
o.open("game.out");
o<<kq;
o.close();
return 0;
}
and the result was not correct. Then I decided to copy these codes to Visual Studio 2019 and it gave me the correct result. I don't know what happened. I copied the same codes from CodeBlocks to VS and the results were completely different.
Welcome to the wonderful world of undefined behavior.
You use the contents of the array game before it's initialized. Local variables are not initialized, their contents is indeterminate and using indeterminate values lead to undefined behavior.
If you want the array to be initialized to all zeroes you need to do it explicitly:
int game[100] = { 0 };
Related
I think I've written correct code but I am not able to input anything. Please help me.
Question - https://codeforces.com/problemset/problem/25/A
My code-
#include<iostream>
using namespace std;
int main(){
int n,count(0);
int arr[n];
for(int i=0;i<n;++i){
cin>>arr[i];
}
for(int i=2; i<n; ++i){
if(arr[i]-arr[i-1] != arr[i-1]-arr[i-2]){
++count;
}
}
cout<<count<<endl;
return 0;
}
It's probably undefined behavior from your int n variable being uninitialized. Initializing n to an explicit value allows for input as intended.
The following is the code, i have tried al the other styles mentioned in the comments one by one but none worked to remove the garbage value, Although "jugaad" works but it's not performance wise good. Couldn't figure out what's wrong !
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
// Other styles
// int data[n+1][m+1] = {0,0};
// int data[n+1][m+1] = {0};
// int data[n+1][m+1] = {{0,0}};
int data[n][m] = {0};
// Jugaad start:
for(int i=0;i<m;i++){
data[0][i] = 0;
}
// Jugaad end
cout<<"\n";
for(int x=0;x<n;x++){
for(int y=0;y<m;y++){
cout<<data[x][y]<<"\t";
}
cout<<"\n";
}
cout<<"\n";
return 0;
}
Screenshot of code and output
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
data[i][j] = 0;
}
``}
This will set all values of your array to zero
While practicing c++ code, I used the variable that was declared in the for loop.i want it to use it again in another for loop. But it showed me an error that the
variable i was not declared in scope
and I tried the same loop in Eclipse IDE it showed me
the symbol i was not resolved.
The sample code looks similar to this:
#include<iostream>
using namespace std;
int main(){
for(int i=0;i<10;i++){
cout<<i;
}
for(i=10;i<20;i++){
cout<<i;
}
}
You have to declare the variable for each scope:
#include<iostream>
using namespace std;
int main(){
for(int i=0;i<10;i++){
cout<<i;
}
for(int i=10;i<20;i++){
cout<<i;
}
}
After the first loop, there is no i anymore. You can try what the compiler says and see that this will fail:
int main(){
for(int i=0;i<10;i++){
cout<<i;
}
cout<<i; // Error
}
i only defined within the scope of the first for loop, so it needs to be re-declared in the second one.
Early Microsoft C++ compilers had a bug where the i leaked into the scope of the for loop to produce effectively
int i;
for (i = 0; i < 10; i++){
By writing for(int i=0; i<10; i++) {...} you declare the int i inside the for loop scope and it only has effect inside the for loop.
If you want to re-use the int i then you should place it outside of & before any for loop:
#include<iostream>
using namespace std;
int main(){
int i = 0;
for(i=0; i<10; i++){
cout<<i;
}
for(i=10; i<20; i++){
cout<<i;
}
cout<<i; // <- fine, 20
}
Or, simply repeat the declaration with each for loop then int i in for loops are totally different variables.
#include<iostream>
using namespace std;
int main(){
for(int i=0; i<10; i++){
cout<<i;
}
for(int i=10; i<20; i++){
cout<<i;
}
//cout<<i; <- oops!!! error
}
I have written a code in C++ and tried to compile it using MinGW and TDMGCC compiler. It get compiles very well. But the problem occurs when i tried to use .exe file generated by these compiler and i give a large input set to my program my dos stops working and says windows has stopped working. After clicking on OK it terminates my execution. How can i fix it.
Here is my code
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int t,n,k,c,x,w;
ofstream myfile;
myfile.open ("example.txt");
cin>>t;
for(w=1;w<=t;w++)
{
cin>>n>>k>>c>>x;
int a[n],b[n],array[n][n];
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
array[i][j]=(a[i]*i+b[j]*j+c)%x;
}
}
int re[1000],z=0;
if(k>1){
for(int i=1;i+k-1<=n;i++)
{
for(int j=1;j+k-1<=n;j++)
{
re[z]=array[i][j]+array[i][j+1]+array[i+1][j]+array[i+1][j+1];
z++;
}
}}
else
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
re[z]=array[i][j];
z++;
}
}
}
int lar=re[0];
for(int i=1;i<z;i++)
{
if(re[i]>lar)
lar=re[i];
}
myfile<<"Case #"<<w<<": "<<lar<<endl;
}
myfile.close();
return 0;
}
You have a basic off-by-one error:
int a[n],b[n],array[n][n];
for(int i=1;i<=n;i++)
cin>>a[i];
a has length n, meaning valid indices are between 0 and n-1 inclusive. You're writing a value at index n, which is past the end.
Structure your loops like this instead:
for (int i = 0; i < n; i++)
cin >> a[i];
All your loops show this problem.
Also, like #NathanOliver says, use std::vector instead of variable-length arrays (VLA). VLA is in the C standard, but it's not in C++. So your use of it here is non-standard. Simply add #include <vector> and replace
int a[n];
with
std::vector<int> a(n);
and everything else can stay the same.
This is my first time here. I really hope anyone can help me out there. So this is my problem. I keep getting run time error #2 something about a corrupt "arr". But the program runs fine until the end. I can't figure it out.
This is my code:
#include <iostream>
using namespace std;
void main(){
int arr1[3];
int temp;
//INPUT NUMBERS
for (int i=0; i<5;i++)
{
cin>>arr1[i];
}
cout<<endl;
//SORT
for(int c=0;c<5;c++)
{
for (int k=0;k<5;k++)
{
if(arr1[c]<arr1[k])
{
temp=arr1[k];
arr1[k]=arr1[c];
arr1[c]=temp;
}
}
}
for (int m=0; m<5; m++)
{
cout<<arr1[m]<<endl;
}
}
Try this out:
#include <iostream>
using namespace std;
int main()
{
int arr1[5];
int temp;
//INPUT NUMBERS
for (int i = 0; i < 5; i++) {
cin >> arr1[i];
}
cout << endl;
//SORT
for (int c = 0; c < 5; c++) {
for (int k = 0; k < 5; k++) {
if (arr1[c] < arr1[k]) {
temp = arr1[k];
arr1[k] = arr1[c];
arr1[c] = temp;
}
}
}
for (int m = 0; m < 5; m++) {
cout << arr1[m] << endl;
}
}
It compiles properly without any errors. The mistake you had made is in declaring the size of the array. If you want to store 5 in puts, you need to declare an array of size 5. Your code might work, but a good compiler will always give out an error.
The reason being that when you declare an array, you actually create a pointer to the first element of the array. And then, some memory regions are kept for this array, depending on the size. If you try to access an element that is outside these memory regions, you may encounter a garbage value.
Here's your code in ideone.