I am trying to print a pseudo-multidimensional array(don't ask why XD), but for some reason when i do this
#include <iostream>
#define Row_sz 3
#define Col_sz 4
using namespace std;
int main()
{
int row, col;
int arr[Row_sz*Col_sz];
cout<<"Printing a multi-dimensional array."<<endl;
do{
cout<<"Enter the number of rows: "<<endl;
cin>>row;
}while(row>Row_sz||row<0);
do{
cout<<"Enter the number of columns: "<<endl;
cin>>col;
}while(col>Col_sz||col<0);
for (int x=0;x<row;x++){
for(int y=0;y<col;y++){
cout<<"Enter the value: "<<endl;
cin>>arr[x*y];
}
}
cout<<"The array matrix: "<<endl;
for (int x=0;x<row;x++){
for(int y=0;y<col;y++){
cout<<arr[x*y]<<" ";
}
cout<<endl;
}
}
if I enter for example :5,4,3,2,1,6,7,8,9,11,12,13 I get
9 9 9 9
9 6 11 8
9 11 12 13
Instead of :
5 4 3 2
1 6 7 8
9 11 12 13
or something like that.
replace
x*y
with
x*Col_sz+y
The * operator is multiplication. Your array is 12 elements long. You want to fill in elements 0,1,2,3,4,5,6 ... 11. If you look at what x*y produces, you'll see that isn't what you want.
Related
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've got a little problem with my program and I can't solve what's wrong. Basically, there are two arrays sorted in ascending order and I have to merge them into one.
I expect the output to be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
But it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 0
What am I doing wrong, that the last value in the output isn't 14? I think that the solution to this will be very simple, but I can't figure it out.
Here's the code:
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int arrA[]={1,3,5,7,9,11,13};
int arrB[]={2,4,6,8,10,12,14};
int arrC[sizeof(arrA)/sizeof(int)+sizeof(arrB)/sizeof(int)];
int sizeA=sizeof(arrA)/sizeof(int);
int sizeB=sizeof(arrB)/sizeof(int);
int sizeC=sizeof(arrC)/sizeof(int);
for (int i=0;i<sizeA;){
for (int j=0;j<sizeB;){
if (arrA[i]<=arrB[j]){
arrC[i+j]=arrA[i++];
}
else{
arrC[i+j]=arrB[j++];
}
}
}
for (int i=0; i<sizeC; i++){
cout << arrC[i] << " ";
}
return 0;
}
Actually you never get to the point where You assign value to arrC[13] which is the last 14 element. In last iteration for your outer loop i==6 and the same for the inner loop. So you end when i+j is equal to 12.
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.
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.