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!!
Related
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...
// 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 have to write a program that takes a completed sudoku board, saves only the numbers (meaning all the symbols used between the numbers to separate them such as '-', '|' etc cant be saved) into a two-dimensional array.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int input[11] = { 0 };
int sudoku[9][9] = { 0 };
for (int line = 0; line <= 10; line++)
{
cin >> input[line];
}
system("PAUSE");
return 0;
}
This is the only working code I've got so far. I've tried different kinds of for loops to get this done but I can't figure why it doesn't work.
So I wanted to ask, is it even possible save all the numbers of a string into a multi-dimensional array? And if it's not, where is my approach wrong or how could I solve this task?
One example of the input would be:
.5.1.4.|.8.6.9.|.7.2.3
.8.7.2.|.3.4.5.|.6.1.9
.9.6.3.|.2.1.7.|.5.4.8
-------|-------|-------
.6.2.8.|.1.3.4.|.9.5.7
.1.9.7.|.6.5.2.|.8.3.4
.4.3.5.|.7.9.8.|.1.6.2
-------|-------|-------
.2.4.6.|.9.7.1.|.3.8.5
.7.5.1.|.4.8.3.|.2.9.6
.3.8.9.|.5.2.6.|.4.7.1
One approach is to use regular expressions. This way the formatting of the sudoku board can change but your will still be able to parse out the numbers.
The reason I broke it into two for loops was to easily ignore the row that has no numbers in it.
#include <iostream>
#include <regex>
#include <string>
#include <vector>
int main()
{
std::string line;
// this regular expression matches a single digit
std::regex exp("(\\d)");
std::smatch res;
int sudoku[9][9] = {{0}};
int row = 0;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
// get a line of the board
std::getline(std::cin, line);
// search for the next digit in the line
for (int k = 0; std::regex_search(line, res, exp); ++k)
{
// convert the digit into an integer and store it in the board
sudoku[row][k] = std::stoi(res[0]);
// the rest of the line after the first match becomes the new
// line so that we can search for the next digit
line = res.suffix();
}
row += 1;
}
// ignore every third row that is used to separate the board sections
std::getline(std::cin, line);
}
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
std::cout << sudoku[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
For your example board, it produces this output:
5 1 4 8 6 9 7 2 3
8 7 2 3 4 5 6 1 9
9 6 3 2 1 7 5 4 8
6 2 8 1 3 4 9 5 7
1 9 7 6 5 2 8 3 4
4 3 5 7 9 8 1 6 2
2 4 6 9 7 1 3 8 5
7 5 1 4 8 3 2 9 6
3 8 9 5 2 6 4 7 1
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 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.