In the code below, why am I not able to print the elements of vector? Not even their size. Can you please explain the reason?
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,k=2;
cin>>n;
vector< int >v;
while(n!=0){
if((n%k==0)&&(k<10)){
n=n/k;
v.push_back(k);
}
else if((n%k!=0)&&(k<10)){
k++;
}
}
cout<<v.size()<<"\n";
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
}
Related
#include<iostream>
#include<climits>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[n];
} //array instillisation
int cursum=0;
int maxsum=INT_MIN;
for(int i=0;i<n;++i){
cursum+=a[i];
if(cursum<0){
cursum=0;
}
maxsum=max(cursum,maxsum);
}
cout<<maxsum<<endl;
return 0;
}
//this code is for maximum subarray problem using kadane's algo.My compiler is retrurning wrong output
In cin you are doing incorrect operation it should be
cin>> a[i];
what you are doing is taking the value of a[n]
I was trying to modify the selection sort code in c++ to check the results. My modified code is:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter number of elements\n";
cin>>n;
int i,j,small,pos,t;
int a[n];
cout<<"Enter elements of array\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
small=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
t=a[i];
a[i]=a[pos];
a[pos]=t;
}
}
cout<<"Sorted array:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
This code works for certain arrays but does not work for others.
For example: If I enter 1,11,2,22,3 as the array, I get a proper output: 1,2,3,11,22. But, if I enter 1,11,2,22,3,33 as the array, I get the same array as the input as the output. Please tell me what is wrong with my code.
The issue is because of wrong place of the swap logic. The swap should be placed outside the inner loop.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter number of elements\n";
cin>>n;
int i,j,small,pos,t;
int a[n];
cout<<"Enter elements of array\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
small=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
//Swap here
t=a[i];
a[i]=a[pos];
a[pos]=t;
}
cout<<"Sorted array:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Now this gives the output as expected.
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
#include<iostream>
using namespace std;
int main(){
int n,i=0,j=1;
cin>>n;
int m;
while(m!=n){
m=i*j;
cout<<m;
i++;
j++;
}
return 0;
}
I want to display Pronic Number ie . 0,2,6,12,20,30,42... When I insert value of n as Pronic Number Code run fine ... and desire output is given.. Bug is when you insert value of n=15 while loop goes to infinity but i want to display till 15 or less than
15.. Here 15 is not a Pronic Number..
Initialize m before using it, plus change your condition to stop the loop when m>n
#include<iostream>
using namespace std;
int main(){
int n,i=0,j=1;
cin>>n;
int m=0;
while(m<=n){
cout<<m<<" ";
i++;
j++;
m=i*j;
}
return 0;
}
I'm trying to convert this nested for loop to nested while loop:
The program:
#include<iostream>
using namespace std;
void main()
{
int i,j,n,stars=1;
cout<<"Enter the number of rows:";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=stars;j++)
cout<<"*";
cout<<"\n";
stars=stars+1;
}
}
While Trying the nested while loop the loop doesn't stop can someone please give me the solution?
#include<iostream>
using namespace std;
void main()
{
int n,i,j,k,stars=1;
cout<<"Enter the number of rows";
cin>>n;
i=1;
while(i<=n)
{
j=1;
while(j<=stars)
{
cout<<"*";
cout<<"\n";
stars=stars+1;
}
j=j+1;
}
i=i+1;
}
You have to incement your control varibales i ans j inside your loops. You did it outsid the loops directly after. Apart from this the varibale stars was incremented in the outer for loop. In the secend code snippet you did it in the inner while loop. Adapt your code like this:
#include<iostream>
int main()
{
int n;
std::cout<<"Enter the number of rows";
std::cin>>n;
int stars=1;
int i=1;
while ( i<=n ) // corresponds to for(i=1;i<=n;i++) { .... }
{
int j=1;
while ( j<=stars ) // corresponds to for(j=1;j<=stars;j++) cout<<"*";
{
std::cout<<"*";
j++; // increment control variable inside of the loop
}
std::cout<<"\n";
stars++;
i++; // increment control variable inside of the loop
}
return 0;
}
Note if you improve the formatting of the code, you'll find such mistakes easily.