In the input I have to specify the array as well as it's elements and the output should be in the form given below
input
5
2 4 6 8 3
Sample Output
2 4 6 8 8
2 4 6 6 8
2 4 4 6 8
2 3 4 6 8
And this is my output
2 4 6 8 8
2 4 6 6 8
2 4 4 6 8
2 4 3 6 8
2 3 3 6 8
2 3 3 6 8
int main() {
int* a=0;
int n,x;
std::cout<<"Enter size ";
std:: cin>>n;
std::cout<<"Enter elements ";
a=new int[n];
for(int i=0;i<n;i++){
std::cin>>x;
a[i]=x;
}
int q=a[n-1];
for(int i=n;i>=0;i--){
if(a[i-2]>q)
{ a[i-1]=a[i-2];
}else
a[i]=q;
for(int j=0;j<n;j++ )
{ std::cout<<a[j];cout<<" ";
}
cout<<" \n ";
}
//for(int j=0;j<n;j++ ){std::cout<<a[j];}
getch();
}
What am I doing wrong?
Since i goes all the way down to 0. Then i-2 can go to -2. Thus this line indexes before the beginning of the array, which is undefined behavior:
if(a[i-2]>q)
Here you are assigning over the value in the array without remembering what the old value was.
}else
a[i]=q;
Thus you are loosing information and thus something is going to go wrong as a sort is not supposed to destroy information.
Related
// printing in spiral order matrix
#include<iostream>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
int arr[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>arr[i][j];
}
}
// print
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
// spiral print
int row_start=0,row_end=n-1,col_start=0,col_end=m-1;
while(row_start<=row_end && col_start<=col_end){
for(int j=col_start; j<=col_end; j++){
cout<<arr[row_start][j]<<" ";
}
row_start++;
for(int i=row_start; i<=row_end; i++){
cout<<arr[i][col_end]<<" ";
}
col_end--;
for(int j=col_end; j>=col_start; j--){
cout<<arr[row_end][j]<<" ";
}
row_end--;
for(int i=row_end; i>=row_start; i--){
cout<<arr[i][col_start]<<" ";
}
col_start++;
}
return 0;
}
My output is:
PS C:\Users\anmol\Desktop\c++projectwork> ./a
3 4
1 2 3 4 5 6 7 8 9 0 1 2
1 2 3 4
5 6 7 8
9 0 1 2
1 2 3 4 8 2 1 0 9 5 6 7 6
I am getting an extra '6' at the end.
which is not required however this type of problem only come when matrix is rectangular.
But code works fine for square matrix.
Please tell me where i went wrong..
Suppose you have a below matrix. Let's dry run your code on below example.
1 2 3 4
5 6 7 8
9 10 11 12
Dry Run
1st for loop: prints 1 2 3 4 row_start=1,row_end=2,col_start=0,col_end=3
2nd for loop: prints 8 12 row_start=1,row_end=2,col_start=0,col_end=2
3rd for loop: prints 11 10 9 row_start=1,row_end=1,col_start=0,col_end=2
4th for loop: prints 5 row_start=1,row_end=1,col_start=1,col_end=2
All condition of while loop are true 2nd Iteration of while loop:
1st for loop: prints 6 7 row_start=2,row_end=1,col_start=1,col_end=2
2nd for loop: won't do anything row_start=2,row_end=1,col_start=1,col_end=1
3rd for loop: prints 6
DO you see the problem?
you should run 3rd for loop only if
"row_start < row_end"
similarly you should check for
"col_start<col_end"
before running the 4th loop
i tried to write a code that shows the multiplication table of 1-4 but in the first attempt(below) it gave me:
1 2 3 4
2 4 6 8
3 6 9 12
4 0
but then sb told me to write it like this and it worked.
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
**Now my question is what was the problem with the 1st one?????
1st version:
#include <iostream>
using namespace std;
main(){
int x[4][4];
for(int i=1;i<5;i++){
for(int j=1;j<5;j++){
x[i][j]=i*j;
cout<<x[i][j]<<" ";
}
cout<<endl;
}
}
2nd:
#include <iostream>
using namespace std;
main(){
int x[4][4];
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
x[i][j]=(i + 1)*(j + 1);
cout<<x[i][j]<<" ";
}
cout<<endl;
}
}
The difference is the starting index.
Arrays begin at 0 position, so you must initialize your i and j from 0.
Otherwise, you could declare your x matrix as [5][5], and you can use i and j starting from 1.
Cheers!!
I am trying to create an array by taking value 'n' from the console and create an array with 'n' inits and then again take a value 'r' to work.
so far I wrote
int main(){
int n = 0;
cin >> n;
int* a = new int[n];
for(int i = 0; i< sizeof(a);i++){
cin >> a[i];
}
for(int y = 0; y < sizeof(a);y++){
cout << a[y] << " ";
}
int r = 0;
cin >> r;
rotate(a,r);
(the "cout" part is for checking the output of the array)
but no matter I try I would get an array which length doesn't equal the input 'n'. Can anyone give me some advice on it?
Here the outputs for every input from console:
(the second row is supposed to be the created array)
INPUT
6
1 2 3 4 5 6
3
OUTPUT
1 2 3 4 5 6 3 0
INPUT
10
-1 -2 3 4 5 -6 7 -8 9 0
5
OUTPUT
-1 -2 3 4 5 -6 7 -8
INPUT
1
1
1
OUTPUT
1 1 0 0 0 0 135137 0
INPUT
5
1 2 3 4 5
5
OUTPUT
1 2 3 4 5 -3 135137 0
Any ideas why those unexplainable numbers at the end?
As Algirdas said, take a close look at what SizeOf does. Also, you don't really need it. You can make it work like this:
for(int i = 0; i< n; i++){
cin >> a[i];
}
As you've got 'n' elements in your array.
Also, I know that most textbooks are really fond of arrays, but please follow Cody Gray's advice!
I want to write a code that print a table like this:
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
I wrote a code to print a table as said above, but it just print 5's.
I know that I have to use a condition to print such a table. What's the condition to print it ?
int main () {
int number = 5;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (condition)
...
else
cout << number << " ";
}
cout << endl;
}
return 0;
}
As I mentioned in comments, what you want to print is the Chebyshev distance to the center +1. I dont know what condition can make your code work, but instead I would use a simple formula to calculate each value:
#include <iostream>
using namespace std;
int main() {
for (int i=0;i<9;i++){
for (int j=0;j<9;j++){
cout << std::max(abs(i-4),abs(j-4)) +1 << " " ;
}
cout << endl;
}
}
/*To make boxes and inside box and so on
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int a[100][100],n,i,j,k=0,l;
clrscr();
cout<<"Enter the outside No. n \n";
//like for your answer it is 5;
//i and j are loop element for 2D array
//k is used for making n boxes
cin>>n;
l=n;
n=2*n-1;
while(k<n)
{
if(k%2==0)
{
for(i=k;i<n-k;i++)
{
a[0+k][i]=l;
a[i][0+k]=l;
a[n-1-k][i]=l;
a[i][n-1-k]=l;
}
k++;l--;
}
else
{
for(i=k;i<n-k;i++)
{
a[0+k][i]=l;
a[i][0+k]=l;
a[n-1-k][i]=l;
a[i][n-1-k]=l;
}
k++;l--;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout << a[i][j];
if(a[i][j]>9)
cout<<" ";
else
cout<<" ";
}
cout<<endl;
}
getch();
}
This is code for creating vector of size (1,len) for objects:
#include<iostream.h>
class vector
{
int *vect;
int len;
public:
vector(){ vect=NULL; len=0; }
void get_data();
void display();
};
void vector::get_data()
{
cout<<"Enter number of elements: ";
cin>>len;
int *f=new int(len);
vect=f;
cout<<"Enter "<<len<<" values: ";
for(int i=0;i<len;i++) cin>>*(vect+i);
}
void vector::display()
{
for(int i=0;i<len;i++) cout<<*(vect+i)<<" ";
cout<<endl;
}
void main()
{
vector v1,v2;
v1.get_data();
v1.display();
v2.get_data();
v2.display();
v1.display();
}
Output:
Enter number of elements: 5
Enter 5 values: 1 2 3 4 5
1 2 3 4 5
Enter number of elements: 5
Enter 5 values: 6 7 8 9 9
6 7 8 9 9
9 2 3 4 5
Why did the first value of vector object v1 change on creating object v2 ?
When I replaced the line:
int *f=new int(len); in get_data() to int *f=new int[len]; i got the expected result:
Enter number of elements: 5
Enter 5 values: 1 2 3 4 5
1 2 3 4 5
Enter number of elements: 5
Enter 5 values: 6 7 8 9 9
6 7 8 9 9
1 2 3 4 5
new int(len) creates a single integer, and initialises it with the value len.
new int[len] creates an array of len integers, uninitialised.
new int(len) allocates a single int object and initialises it with the value len. On the other hand, new int[len] allocates an array of ints of length len.
Compare with int x(5); and int x[5];.
When you were allocating only a single int, cin>>*(vect+i); was attempting to write to objects that have not been allocated.