difference between new(size) and new[size] - c++

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.

Related

multiplication table of 1-4

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!!

Why is this code outputting so many numbers? [duplicate]

This question already has answers here:
ARRAYSIZE C++ macro: how does it work?
(7 answers)
c++ sizeof(array) return twice the array's declared length
(6 answers)
Closed 3 years ago.
Starting with two arrays a and b, I am trying to output a matrix c with dimensions sizeof(a) and sizeof(b), whose entries are the product of every pair of the Cartesian product of a and b.
Theses products are also stored in a two dimensional array c.
My code is below.
#include <iostream>
#include <string>
int main()
{
int a[]= { 1,2,3,4,5,5 };
int b[]= { 1,23,2,32,42,4 };
int c[sizeof(a)][sizeof(b)];
for (int i = 0; i < sizeof(a); i++) {
for (int j = 0; j < sizeof(b); j++) {
c[i][j] = a[i]* b[j] ;
std::cout << c[i][j] << " ";
}
std::cout << "\n";
}
return 0;
}
My output is:
1 23 2 32 42 4 -858993460 -858993460 1 2 3 4 5 5 -858993460 16710224 15543422 1 2161328 2122464 16710312 15543008 196436084 15536213
2 46 4 64 84 8 -1717986920 -1717986920 2 4 6 8 10 10 -1717986920 33420448 31086844 2 4322656 4244928 33420624 31086016 392872168 31072426
3 69 6 96 126 12 1717986916 1717986916 3 6 9 12 15 15 1717986916 50130672 46630266 3 6483984 6367392 50130936 46629024 589308252 46608639
...
This is just a small part of the output.
sizeof(a) is not the length of the array, it is the number of bytes required to store it.
Since the element type of the array is larger than one byte each, the numbers are different.

Trouble printing a pseudo-multidimensional array

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.

print a table of decremented numbers

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();
}

Insertion sort homework

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.