Answer changes while deleting the vector display piece of code c++ - c++

to resolve a codeforces problem
I have written this c++ code and i have a big problem while displaying the result: in fact,if i add this code :
cout<<"t2simplifier"<<endl;
aff(t2simplifier);
cout<<endl;
aff(t2primsimplifier);
cout<<endl;
the result will be correct
Otherwise it will be wrong
The code:
#include <bits/stdc++.h>
using namespace std;
void aff (vector<int> v)
{
for (int i=0;i<v.size();i++)
cout<<v[i]<<"|";
}
int main()
{
int n;
cin>>n;
int t1[2][n];
vector <int> t2 ;
vector <int> t2prim ;//flous
vector <int> t2simplifier ;
vector <int> t2primsimplifier ;//flous
vector <int> t3prim ;//flous
for(int i=0;i<n;i++)
cin>>t1[0][i];
for(int i=0;i<n;i++)
cin>>t1[1][i];
for (int i = 0;i<n;i++)
for (int j = i+1 ;j<n ;j++)
{
if(t1[0][i]<t1[0][j])
{
t2.push_back(j);
t2prim.push_back(t1[1][i]+t1[1][j]);
}
}
// cout<<"t2"<<endl;
// aff(t2);
// cout<<endl;
// aff(t2prim);
// cout<<endl;
//pour simplifier t2 et t2prim
int minn;
for (int i = 1;i<n;i++)
{
minn==1000000000;
for (int j = 0 ;j<n ;j++)
{
if((t2[j]==i)&&(t2prim[j]<minn))
{
minn=t2prim[j];
}
}
t2simplifier.push_back(i);
t2primsimplifier.push_back(minn);
}
cout<<"t2simplifier"<<endl;
aff(t2simplifier);
cout<<endl;
aff(t2primsimplifier);
cout<<endl;
for (int i = 0;i<t2simplifier.size();i++)
for (int j = t2simplifier[i] ;j<n ;j++)
{
if(t1[0][t2simplifier[i]]<t1[0][j])
{
t3prim.push_back(t2primsimplifier[i]+t1[1][j]);
}
}
// cout<<"t3prim";
// aff(t3prim);
if (t3prim.size()==0)
cout<<-1;
else
{
//talla3 min
int k = t3prim[0];
for (int i = 1;i<t3prim.size();i++)
{
if(k>t3prim[i])
k=t3prim[i];
}
// k is the result
cout << k ;
}
return 0;
}
input :
5
2 4 5 4 10
40 30 20 10 40
the result with the part of code is :
t2simplifier
1|2|3|4|
70|50|50|50|
90
but when we delete this piece of code(of vector display)
the result will be wrong :
24

Thanks to Mr "François Andrieux"
in the code there is a typing error
The line minn == 1000000000;is performing a comparison and it should be minn = 1000000000;
this error leads to an undefined behavior from reading from an uninitialized variable.

Related

How can I start a loop from middle of the array?

There is a given pseudo code.
int Function(X : array[P..Q] of integer)
1 maxSoFar = 0
2 for L = P to Q
I was using C and trying to change it into C++ code.
The reason that I am struggling with it is I have no idea how to express it is start from 'P' in the loop.
I do not know how to do that before initilization even without parameter.
int Function(vector<int> X)
{
int maxSoFar = 0;
for (int I = 0; I < X.size(); I++) ;
}
This is what I did.
I wonder if it is same with the pseudo code or not.
You can just divide your vector size by 2:
#include <vector>
#include <iostream>
using namespace std;
int f(vector<int> v) {
int maxSoFar = 0;
int len = v.size();
for (int i = len / 2; i < len; i++) { // i = len / 2, starts from the middle
maxSoFar += v[i];
cout << v[i] << ' ';
cout << maxSoFar << endl;
}
}
int main(void) {
vector<int> v = {1, 3, 5};
f(v);
}
output:
3 3
5 8
You should take a closer look at the for loop on wikipedia: for (initialization; break condition; incrementation)
#include<iostream>
using namespace std;
int main()
{
int i,n;
cout<<"enter the number of elements : ";
cin>>n;
int a[n];
cout<<endl<<"enter the value of this array : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<endl<<"print the last half elements of this array : ";
for(i=(n/2);i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}

Swapping all smallest and largest number in an array/vector

I was trying to make a program that swaps all the biggest and smallest numbers in an array or a vector. I came up with a program but for some reason I'm not able to debug it to get the problem. Its not printing the vector, neither do I know what the issue is. Can anyone please help me.
Desired Input and Output
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int n;
cin>>n; //size input
vector<int> arr;
for(int i=0;i<n;i++) //filling up the vector
{
int input;
cin>>input;
arr.push_back(input);
}
vector<int> arr1=arr; //copying the vector
sort(arr1.begin(), arr1.end()); //sorting the new vector
int i=0,j=n-1,i1=0,j1=n-1; //i and j are for the vector arr & i1 and j1 are for vector arr1
while(i1<j1)
{
if(arr1[i1]==arr[i] && arr1[j1]==arr[j]) //if the first and last number of the sorted vector is found in arr the swap
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i1++;
j1--;
i=0; // i and j are set to initial value so that it is checked from the start
j=n-1;;
}
else if(arr1[i1]<arr[i] && arr1[j1]==arr[j]) //if only the biggest place element is found the increase i
{
i++;
}
else if(arr1[i1]==arr[i] && arr1[j1]>arr[j]) //if only the smallest place element is found the decrease j
{
j--;
}
else if(arr1[i1]!=arr[i] && arr1[j1]!=arr[j]) //if none of them are found then increase i and decrease j
{
i++;
j--;
}
}
for(int f=0;f<n;f++) //print the vector
cout<<arr[f]<<" ";
return 0;
}
/*
Sample input 1
6
12 34 87 56 38 98
Sample output 1
98 87 34 38 56 12
Sample input 2
6
8 7 9 2 4 6
Sample output 2
4 6 2 9 8 7
*/
Some help would be really appreciated.
A simpler method would be to make a vector of indexes rather than sorting the vector and trying to find the values in the original vector. For example:
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
int n;
std::cin >> n;
std::vector<int> arr;
std::vector<int> indexes;
for (int i = 0; i < n; i++)
{
int input;
std::cin >> input;
arr.push_back(input);
indexes.push_back(i);
}
std::sort(indexes.begin(), indexes.end(), [&](int a, int b) {return arr[a] < arr[b]; });
for (int i = 0, j = n - 1; i < n / 2; i++, j--)
{
std::swap(arr[indexes[i]], arr[indexes[j]]);
}
for (int f = 0; f < n; f++)
{
std::cout << arr[f] << " ";
}
return 0;
}

spiral matrix breaks on the last "for" loop and doesn't show anything

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the size of the array :";
cin>>n;
int A[n][n];
int y=n,k=1,p=0,i;
while(k<=n*n)
{
for(i=p;i < y;i++)
{
A[y-1][i]=k++;
}
for(i=y - 2;i > p;i--)
{
A[i][y-1]=k++;
}
for(i=y - 2;i > p;i--)
{
A[p][i]=k++;
}
for(i = p + 1;i < y; i++)
{
A[i][p]=k++;
}
p++;
y--;
}
if(!n%2)
{
A[(n+1)/2][(n+1)/2]=n*n;
}
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<A[i][j]<<"\t";
}
cout<<endl;
}
return 0;
I need to do a spiral matrix the way like this > enter image description here.
It breaks on the last "for" cycle and just doesn't show anything;;; Still, it shows up if I'm replacing one of the loop's statements;; I would be grateful if you point me where's my mistake!
(this code is a modified one brought from here https://www.includehelp.com/cpp-programs/print-a-spiral-matrix.aspx)
There were simply some little mistakes on the bounds of the loops (the bounds of the spiral). Here is a slightly modified programme.
PS: Note that you should avoid to use VMA int A[n][n] which is C, not C++.
#include<iostream>
//using namespace std;
int main()
{
int n;
std::cout << "Enter the size of the array :";
std::cin >> n;
int A[n][n];
int y = n, k = 1,p = 0,i;
while(k<= n*n)
{
for(i=p;i < y;i++)
{
A[y-1][i]=k++;
}
for(i=y - 2;i >= p;i--)
{
A[i][y-1]=k++;
}
for(i = y - 2;i >= p;i--)
{
A[p][i]=k++;
}
for(i = p + 1;i < y-1; i++)
{
A[i][p]=k++;
}
p++;
y--;
}
if(!n%2)
{
A[(n+1)/2][(n+1)/2]=n*n;
}
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
std::cout<<A[i][j]<<"\t";
}
std::cout << "\n";
}
return 0;
}

Trying to create a program that reads an array and prints the prime numbers

This is what I wrote so far, where did I go wrong?
#include <iostream>
using namespace std;
int main()
{
int x[5], count=0;
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{
for (int n=2;n<x[i];n++)
{
if (x[i]%n==0)
count++;
}
if (count==1)
cout<<x[i]<<" ";
}
}
Edit:
Many thanks to everyone that tried to help. The problem was that I had to int count in the loop so that it would start from 0 every time. Here's my new working code:
#include <iostream>
using namespace std;
int main()
{
int x[5];
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{ int count=0;
for (int n=2;n<=x[i];n++)
{
if (x[i]%n==0)
count++;
}
if (count==1)
cout<<x[i];
}
}
#include <iostream>
using namespace std;
int main()
{
int x[5], count=0;
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{
bool check = true;
for (int n=2;n<x[i];n++)
{
if (x[i]%n==0)
{
check = false;
break;
}
}
if (check)
cout<<x[i]<<" ";
}
}
For better complexity, change your nested for loop to :
for (int n=2;n<=sqrt(x[i]);n++)
Other way :
int main ()
{
for (int i=2; i<100; i++)
{
bool check=true;
for (int n=2; n*n<=x[i]; n++)
{
if (x[i] % n == 0)
{
check=false;
break;
}
}
if(check) cout << x[i] << " ";
}
return 0;
}
Here's my solution.
I basically assume all numbers are prime, but if I find them not to be prime I don't print them out.
int main() {
bool primeNumber;
int length = 0;
int x[5], count=0;
for (int i=0;i<5;i++){
length++;
cin>>x[i];
}
for (int i = 0; i < length; i++ )
{
primeNumber = true;
for (int j = 3; j <= x[i]/2; j += 2 )//Not necessary to check agains numbers higher than half the number you want to check
{
if (x[i] % j == 0)
{
primeNumber = false;
}
}
if(primeNumber){cout << "Primenumber: " << x[i] << endl;}
}
}
EDIT:
Here's a comment on your code
Let's say your array looks like this
{5,9,13,15, 17}
int main()
{
int x[5], count=0;
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{
for (int n=2;n<x[i];n++)
{
Here you loop from 2-4(2,3,4). It's only necessary to loop half the number you want to check. e.g. (x[i]/2), but you can do it like you have done here.
if (x[i]%n==0){
count++;}
When your number is 5 and you check agains n numbers This will produce 5%2=1,5%3=2,5%4=1. But 5 is a prime number and your code didn't manage to detect that.
}
if (count==1)
Let's say you did find that 5 is a prime above and you managed to increment count to 1. What happens when you find another prime number in your array e.g. 7. That will make count be equal to 2. But you only check against 1, so therefor your next prime number doesn't get printed because count will only be equal to 1 after the first prime number and not the rest.
cout<<x[i]<<" ";
}
}
Hopefully you understood my explanation. It always helps to go over your code by hand or debug mode and think what the code will do with different numbers.
All numers are divideable by itself. That will make all numbers prime if you try to divide numbers with themselves

Merge sort throwing index out of bounds exception in Xcode

I am trying to implement a simple merge sort for even number of elements using the following code :
#include <iostream>
using namespace std;
void merge(int arr1[10],int len1,int arr2[10],int len2,int arr3[10],int len3)
{
int i,j,k;
while(i<len1&&j<len2&&k<len3)
{
if(arr1[i]<arr2[j])
{
arr3[k] = arr1[i];
i++;k++;
}
else if(arr1[i]>arr2[j])
{
arr3[k] = arr2[j];
k++;
j++;
}
}
}
void mergeSort(int a[10],int n)
{
int arr1[10],arr2[10];
for(int i=0;i<n/2;i++)
{
arr1[i] = a[i];
}
for(int i=(n/2+1);i<n;i++)
{
arr2[i] = a[i];
}
mergeSort(arr1,n/2);
mergeSort(arr2,n/2);
merge(arr1,n/2,arr2,n/2,a,n);
}
int main() {
int arr[10],n;
cout << "Enter number of elements\n";
cin >> n;
cout<<"Enter elements\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
mergeSort(arr, n);
cout<<"Sorted array is"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
But I am getting a EXC_BAD_ACESS error on the opening brace of the mergeSort() method. I am new to Xcode and not sure on how to fix this. How do I fix this ?
Thanks !
You have forgot to initialize the int variables i, j, k to 0 (zero) in the function definition for merge(...).
void merge(int arr1[10],int len1,int arr2[10],int len2,int arr3[10],int len3)
{
// int i,j,k; // Not initialized.
int i = j = k = 0;
while(i<len1&&j<len2&&k<len3)
{
...
...
}
After Edit:
void mergeSort(int a[10],int n)
{
int arr1[10],arr2[10];
for(int i=0;i<n/2;i++)
{
arr1[i] = a[i];
}
// Here you are skipping the middle index.
// Let's say array is of length 10, then arr1 contains elements on index from 0 to 4 (5 elements),
// whereas arr2 contains elements on index from 6 to 9 (4 elements).
// 5th index element is missing.
// for(int i=(n/2+1);i<n;i++)
for(int i=(n/2);i<n;i++)
{
arr2[i] = a[i];
}
mergeSort(arr1,n/2);
mergeSort(arr2,n/2);
merge(arr1,n/2,arr2,n/2,a,n);
}
Hope it helps!