I wrote a program of Insertion sort in c++ but output screen does not showing any Statement.I am facing this problem in few another program.
This is the code:
#include<iostream>
using namespace std;
int main()
{
int n;
int arr[n];
cout<<"Enter the number of the element of the Array:";
cin>>n;
cout<<"Enter the element of the Array:";
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<n;i++){
int current=arr[i];
int j=i-1;
while(arr[j]>current && j>=0){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=current;
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}cout<<endl;
return 0;
}
You are using the value of n before it has a value. Change your code to this
int n; // n does not have a value
cout<<"Enter the number of the element of the Array:";
cin>>n; // n gets value here
int arr[n]; // n is used here
You cannot use any variable before it has a value. That's a fundamental rule of C++.
You cannot creat variable C-style arrays during run time.
You should either create them with the new operator or switch to STL containers like std::vector.
Related
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.
This is a syntax of a program that I made of hackerrank to reverse an array. The location of the question is Practice < Data Structures < Arrays < Arrays - DS
the program seems to work fine on an online compiler but is showing error over hackerrank.
can anyone guide me where I went wrong?
#include<iostream>
using namespace std;
int main(){
int n,i;
int arr[n];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=n-1;i>=0;i--)
{
cout<<arr[i];
}
}
You are creating an array with n elements before you read n, i.e n is having garbage value. Here is correct working code.
#include <iostream>
using namespace std;
int main(){
int n,i;
cout<<"Enter number of elements"<<endl; // Comment it if you don't want debug console outut
cin>>n;
int arr[n];
cout<<"Enter "<<n<<" elements"<<endl; // Comment it if you don't want debug console outut
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=n-1;i>=0;i--)
{
cout<<arr[i];
}
}
I changed your solution and it work fine now , i will tell you some notes for your future:
1 - You have to read size of array first before declare array as mentioned in comments by #HolyBlackCat , otherwise it will give garbage value.
2 - this command int arr[n]; won't work on all Compilers and may give you an Error. Is there a solution to declare array without specific size ? Yes , you can use Vector like that vector<int> arr(n);but you have to include library of vector first #include<vector>
3 - In C and C++ programs the main function is of type int and therefore it should return an integer value. The return value of the main function is considered the "Exit Status" of the application. On most operating systems returning 0 is a success status like saying "The program worked fine". So you have to write return 0; in the end of your program.
#include<iostream>
#include<vector> // must include to use Vectors
using namespace std;
int main(){
int n;
cin >> n; // Must read size of array first , otherwise it will give it garbude value
vector<int> arr(n); // instead of int arr[n]
for(int i = 0 ; i < n ; i++)
cin >> arr[i];
for(int i = n-1 ; i >= 0 ; i--)
cout << arr[i] << " ";
cout << endl; // to print new line as mentioned in the problem statement
return 0; // Prefer to use it always
}
Hope that helps you and Good Luck.
The exercise of Hacker Rank needs that you reverse an array of integers, might you have a problem with the variable size array, cause some compilers support this characteristic but others not.
And Hacker Rank do not support some properties in other languages like JS , Hacker Rank do not support prompt() for take an input.
In any case for fix this error you can use vector class.
#include<iostream>
#include<vector>//Include library vector for dynamic size of array
using namespace std;
int main(){
int n;
cin>>n;
vector<int> arr(n);//Declare the vector with a max size n
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i = n-1 ;i>=0;i--){
cout<<arr[i]<<" ";
}
return 0;
}
This code has passed al test cases of hackerRank
All the test on hackerRank had passed
I am writing a program to find if a given sum is possible from a subset of an array. (Subset sum problem). Although many implementations are available on the internet, I have chosen to code it myself. please suggest appropriate corrections. I am doing this using dynamic programming.
#include<iostream>
using namespace std;
bool subsetsum(int a[],int n,int s){
bool T[n][s];
for(int j=0;j<n;j++){
T[j][0]=true;
}
for(int k=1;k<n;k++){
T[0][k]=false;
for(int l=1;l<n;l++){
if((a[k]<s) && ((subsetsum(a,n-1,s-a[k])) || (subsetsum(a,n-1,s)))){
T[k][l]=true;
}
else{
T[k][l]=false;
}
}
}
return T[n][s];
}
int main(){
int n,s;
int a[n];
cout<<"Enter number of elements:\n";
cin>>n;
cout<<"Enter sum required\n";
cin>>s;
cout<<"Enter elements:\n";
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<subsetsum(a,n,s);
}
The output I obtained is below:
Enter number of elements:
3
Enter sum required
5
Enter elements:
1
4
2
0
I do not understand why I obtained 0 as the result when boolean should have been returned.
array is declared as follows:
bool T[n][s];
and return is done as:
return T[n][s];
Probably, you need to do as follows?
return T[n-1][s-1];
Array indexing in C++ starts from 0.
I am trying to use bubble sort method for array with dynamically determined size. Here's the code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter n";
cin>>n;
int arr[n],swap;
cout<<"Enter number"<<endl;
cin>>arr[n];
for(int i=0;i<n-1;i++)
for(int j=0;i<n-i-1;j++)
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
for(int k=0;k<n;k++)
cout<<"arr["<<k<<"]="<<arr[k]<<endl;
return 0;
}
When I define the elements of the array in this way the program works:
const n=5;
int arr[n]={1,2,3,4,5)
But I need to enter the size of the array (n) and its elements from the keyboard. But when I run my code the program crashes after I enter the first number. Is there a way to fix it?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n, swap, temp;
vector<int> arr;
cout<<"Enter n";
cin>>n;
// Loop and accept the n values.
// You may need to take care of the new line.
for(int i = 0; i < n; ++i)
{
cout << "Enter a number : ";
cin >> temp;
arr.push_back(temp);
}
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
for(int k=0;k<n;k++)
cout<<"arr["<<k<<"]="<<arr[k]<<endl;
return 0;
}
Notice how a loop is used to extract n values from user. Also using a std::vector relieves you from writing code for a runtime sized array with new and delete.
Also, you inner loop was checking i<n-i-1 and incrementing j which should be j<n-i-1 instead, otherwise j will increment indefinitely till INT_MAX.
The key word is dynamic allocation. In C, the function is malloc. In Cpp, it can be new and delete. Although a vector can work well, it is just a kind of method by STL. Notice that my code may have safe problem.
#include <iostream>
using namespace std;
int main()
{
int n,temp;
cout<<"Enter n:";
cin>>n;
//dynamic allocation
int *arr=new int[n];
cout<<"Enter number."<<endl;
for(int i=0;i<n;i++){
cin>>arr[i];
}
//bubble sort
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
//output the array
for(int i=0;i<n;i++){
cout<<"arr["<<i<<"]="<<arr[i]<<endl;
}
delete [] arr;
return 0;
}
You can't take an integer array like that, you need to run a loop. You can take a string like that. There are lot of errors in the bubble sort logic as well, try the below code snippet. It should work fine. You need to dynamically allocate the array for arr
int n,r,swap,i,*arr;
cout<<"Enter n\n";
cin>>n;
arr = (int *)malloc((n)*sizeof(int));
cout<<"Enter numbers\n"<<n<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)//You're checking for i. you need to check for j
{
if(arr[j+1]<arr[j])
{
swap=arr[j];
arr[j]=arr[j+1];
arr[j+1]=swap;
}
}
}
//now print your arr
Includes : #include<stdlib.h>
As mentioned in my comment, you CANNOT dynamically declare the size of arrays in C++ (use std::vector if you are willing to achieve that).
You can then do this:
....
cin >> n;
vector<int> arr(n); // reserves space for `n` integers in the memory
....
this is my code, PLease help me ! im using xcode.. i want to generate a sequence for a polynomial and the terms are xor'ed and made a feedback to the first input bit since it is 8 bit it is done 2^8-1 times.Alternate code will also be helpful Thanks in advance
#include "32bit.h"
#include<iostream>
using namespace std;
int main()
{
bool input[8];
int n;
bool out=0;
cout<<"Enter the no of terms ";
cin>>n;
int temp1[n];
int gen=0;
bool store[255];
cout<<"Input power of x in increasing order, Omit x^0";
for(int i=0;i<n;i++)
cin>>temp1[i];
cout<<"Enter key to generate ";
cin>>gen;
for(int m=0;m<255;m++)
{
store[m]=input[gen];
bool temp2[n];
int var=0;
for(int j=0;j<n;j++)
{
var=temp1[j];
temp2[j]=input[var];
}
int c=0;
for(int k=0;k<n;k++)
{
if(temp2[k]%2==1)
c++;
}
if(c%2==1)
out=1;
else
out=0;
for(int l=0;l<8;l++)
input[l+1]=input[l];
input[0]=out;
}
for(int p=0;p<255;p++)
cout<<store[p];
}
There is an out of bounds array access here:
for(int l=0;l<8;l++)
input[l+1]=input[l];
since input is only of size 8 and you are trying to write to input[8] (i.e. the non-existent 9th element) on the last iteration of this loop. I'm guessing it should probably be:
for(int l=0;l<7;l++)
input[l+1]=input[l];