Ordered merge problems - c++

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.

Related

Print common elements in an array intersection

Given two arrays - array arr and array art of size n and m respectively. We have to find intersection of arrays
My solution -
#include<iostream>
#include<climits>
using namespace std;
void inputarray(int arr[],int size){
for(int i=0;i<size;i++){
cin>>arr[i];
}
}
void logic(int arr1[],int size1,int arr2[],int size2){
for(int i=0;i<size1;i++){
int element = arr1[i];
for(int j=0;j<size2;j++){
if(element==arr2[j]){
cout<<element;
arr2[j]=INT_MIN;
break;
}
}
}
}
int main(){
int arr1[100];
int arr2[100];
int size1;
cin>>size1;
int size2;
cin>>size2;
inputarray(arr,size1);
inputarray(arr,size2);
logic(arr1,size1,arr2,size2);
}
But for this abovw solution answer is coming wrong.
Answer Coming is -
6
4
1 2 2 2 3 4
2 2 3 3
2233
Expected Answer is -
6
4
1 2 2 2 3 4
2 2 3 3
223
So please tell where is the problem and how can i solve ?
Errors like this one show how important variable naming is...
In the line:
if(ele==arr[j]){
you are mistaking arr with art...

Spiral matrix, i am getting extra elements while printing spiral matrix order. Don't know why?

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

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.

Finding the number of occurences of an integer in multidimensional array in C++

I wish to find the number of occurrences of a number taken as input in the given multidimensional array defined by the logic below:
...
int n,x,count=0;
cin>> n >> x;
int a[n][n] ;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
a[i][j]= i*j ;
}
}
for( int i=1;i<=n;i++)
{
for( int j=1;j<=n;j++)
{
if(a[i][j] == x)
++count;
}
}
cout<< count ;
...
For eg., if I give input as 6(n) and 12(to find its number of occurrences, x here). The multidimensional array looks something like this:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
Now, the number of occurences of 12 here is 4(count).
But when I give n as 10 and x as 5, the program stops working. I can't seem to find what is happening. Can someone help me on this?
Also in what way can I modify my code?
How can I handle the case when n is as large as 1000 or 10k without changing the logic of the program?
Indices in C/C++ starts at 0. If an array is declared to have size n as in int a[n] the only valid indices are: 0,1,...,n-1 i.e. [0,n[
If you go out of bound undefined behaviour is expected. That should be your case.
Fix the loops as follows (note the new bounds and the +1 in i and j)
int a[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]= (i+1)*(j+1) ;
#include <algorithm>
// ...
for(int i = 0; i < n; ++i) {
count += std::count(a[i], a[i] + n, x);
}
or a more simpler version:
std::cout << std::count(a[0], a[0] + n*n, x);