Lets suppose we have a 5 X 5 random array
1 2 3 7 8
4 7 3 6 5
2 9 8 4 2
2 9 5 4 7
3 7 1 9 8
Now I want to print the right side of the diagonal shown above, along with the elements in the diagonal, like
----------8
--------6 5
------8 4 2
---9 5 4 7
3 7 1 9 8
The code I've written is
#include <iostream>
#include <time.h>
using namespace std;
int main(){
int rows, columns;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter colums: ";
cin >> columns;
int **array = new int *[rows]; // generating a random array
for(int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int)time(NULL)); // random values to array
for(int i = 0; i < rows; i++){ // loop for generating a random array
for(int j = 0; j < columns; j++){
array[i][j] = rand() % 10; // range of randoms
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Max: " << endl;
for(int i = 0; i < rows; i++){//loop for the elements on the left of
for(int j = columns; j > i; j--){//diagonal including the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for(int i = rows; i >= 0; i++){ //loop for the lower side of
for(int j = 0; j < i - columns; j++){ //the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
return 0;
}
After running the code the shape I get is correct , but the elements do not correspond to the main array. I have no idea what the problem is.
Left side:
for (size_t i = 0; i < rows; i++) {
for(size_t j = 0; j < columns - i; j++) {
cout << array[i][j] << " ";
}
cout << "\n";
}
Right side:
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < columns; j++) {
if (j < columns - i - 1) cout << "- ";
else cout << vec[i][j] << " ";
}
cout << "\n";
}
Related
the problem arises when I use a different number of rows and columns, for example, 2 by 3 otherwise, it is running okay. the sum of column outputs garbage values
I can't seem to understand where the bug is.
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int a[10][10];
int i,row,column, j, s = 0, sum = 0;
cout<<"Enter Number of rows: ";
cin>>row;
cout<<"Enter Number of columns: ";
cin>>column;
cout<< "Enter elements Matrix \n";
for (i = 0; i < row; i++)
for (j = 0; j < column; j++)
cin >> a[i][j];
cout << "Matrix Entered By you is \n";
for (i = 0; i < row; i++)
{
for (j = 0; j <column; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (i = 0; i < row; i++)
{
for (j = 0; j <column; j++)
s = s + a[i][j];
cout << "Sum of Row " << i + 1 << " is: " << s;
s = 0;
cout << endl;
}
cout << endl;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
s = s + a[j][i];
cout << "Sum of Column " << i + 1 << " is: " << s;
s = 0;
cout << endl;
}
}
You are not iterating correctly to get your columns sum, column and row are switched up. change to:
for (i = 0; i < column; i++) // <-----
{
for (j = 0; j < row; j++) // <-----
s = s + a[j][i];
cout << "Sum of Column " << i + 1 << " is: " << s;
s = 0;
cout << endl;
}
Consider a 3x4 matrix:
1 2 3 4
1 2 3 4
1 2 3 4
Your current loop would access it in the following manner, invoking undefined behavior.
[1] [2] [3] 4
[1] [2] [3] 4
[1] [2] [3] 4
[?] [?] [?]
I have created a 2d array, so lets say as I did in the code below the array elements shows like this:
3 4
8 2
I want to compare the" ROWS" and get the minimum number, like what I do with 3 & 4, I get 3 is the min number and with the second row between 8 & 2, I get 2 is the min number.
My problem is that I can't get it to compare the columns as in 3 & 8 to get the max out between them, and 4 & 2.
In the code it just outputs the max number in the rows it still comparing the "ROWS"
P.S sorry for the long explanation. I just had to clarify it .
int main()
{
int d;
int max;
//array dimention
cout << "Enter the array dimention :- " << endl;
cin >> d;
//max_min for the final maximum numbers
int max_num[d];
//the 2d array
int arr[d][d];
// array input
cout << "please enter the array elements :- " << endl;
for (int i = 0; i < d; i++) {
for (int j = 0; j < d; j++) {
cin >> arr[i][j];
}
}
//array output
cout << "the array elements you enterd :-" << endl;
for (int i = 0; i < d; i++) {
for (int j = 0; j < d; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
//the max numbers
for (int i = 0; i < d; i++) {
max = arr[0][i];
for (int j = 0; j < d; j++) {
if (max < arr[j][i]) {
max = arr[j][i];
}
}
max_num[i] = max;
}
cout << "the maximum elements in array :-" << endl;
for (int i = 0; i < d; i++) {
cout << max_num[i] << endl;
}
}
Im trying to create a program that creates two 2d dynamic arrays and multiply them and give an output and calculate the time taken for the multiplication process based on the input size n. This code works when it comes to outputs lesser than 7 rows and 7 cols but gives an error when the number goes above 8.
using namespace std;
int m1c , m1r , m2c , m2r , i , j , k , l;
int** arr1 = new int*[m1c];
int** arr2 = new int*[m2c];
int** multArr = new int*[m1c];
int main(){
cout << "Enter the Number of rows for matrix 1 :";
cin >> m1c;
cout << "Enter the Number of columns for matrix 1 :";
cin >> m1r;
cout << "Enter the Number of rows for matrix 2 :";
cin >> m2c;
cout << "Enter the Number of columns for matrix 2 :";
cin >> m2r;
for (i = 0; i < m1r; i++) {
arr1[i] = new int[m1c];
multArr[i] = new int[m1c];
}
for (i = 0; i < m2r; i++) {
arr2[i] = new int[m2c];
}
if (m1r != m2c) {
cout << "Number of rows in the first matrix must be equal to the numbr of columns in the second matrix ";
return -1;
}
for (i = 0; i < m1r; i++) {
for (j = 0; j < m2c; j++) {
arr1[i][j] = rand() % 100;
}
}
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
arr2[i][j] = rand() % 100;
}
}
//Displaying the two arrays
for (i = 0; i < m1r; i++) {
for (j = 0; j < m1c; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
delete[] arr1;
delete[] arr2;
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Looks like an error initializing arr1. Your using m2c as the column count. You probably meant m1c.
I want to create a program that take size of rows and columns of 2D array from user, and then take all entries of array from user. And Finally display all the entries from array[0][0] to array[size][size].
My code is:
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
int tom[rows][columns];
cout << "Size of array rows: ";
cin >> rows;
cout << "Size of array columns: ";
cin >> columns;
for(int count1 = 0; count1 < rows; count1++)
{
for(int count2 = 0; count2 < columns; count2++)
{
cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
cin >> tom[count1][count2];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cout << tom[i][j] << endl;
}
}
return 0;
}
Output is:
Size of array rows: 2
Size of array columns: 3
Enter entry of row 0 and column 0: 1
Enter entry of row 0 and column 1: 2
Enter entry of row 0 and column 2: 3
Enter entry of row 1 and column 0: 12
Enter entry of row 1 and column 1: 13
Enter entry of row 1 and column 2: 14
12
13
14
12
13
14
It should give output:
1
2
3
12
13
14
What is the problem?
Please help.
You can't dynamically create an array like this. This shouldn't even compile. And even if it did, you are creating the array before letting the user input the dimension. For the proper Approach use std::vector:
#include <iostream>
#include <vector>
int main()
{
int rows, columns;
std::cout << "Size of array rows: ";
std::cin >> rows;
std::cout << "Size of array columns: ";
std::cin >> columns;
std::vector<std::vector<int>> tom(rows, std::vector<int>(columns));
for (int count1 = 0; count1 < rows; count1++)
{
for (int count2 = 0; count2 < columns; count2++)
{
std::cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
std::cin >> tom[count1][count2];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
std::cout << tom[i][j] << std::endl;
}
}
return 0;
}
output:
Size of array rows: 2
Size of array columns: 3
Enter entry of row 0 and column 0: 1
Enter entry of row 0 and column 1: 2
Enter entry of row 0 and column 2: 3
Enter entry of row 1 and column 0: 12
Enter entry of row 1 and column 1: 13
Enter entry of row 1 and column 2: 14
1
2
3
12
13
14
please don't use using namespace std; - read here why.
You initialized tom before actually getting the number of rows/columns.
#include <iostream>
int main()
{
int rows, columns;
std::cout << "Rows: ";
std::cin >> rows;
std::cout << "Columns: ";
std::cin >> columns;
int arr[rows][columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
std::cout << "Enter the value for [" << i << "][" << j << "] : ";
std::cin >> arr[i][j];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
You cannot declare an array like this:
int tom[rows][columns];
since values of rows and columns are not known yet.
Instead you should initialize this array dynamically after asking for values.
Here is your code fixed:
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
int **tom;
cout << "Size of array rows: ";
cin >> rows;
cout << "Size of array columns: ";
cin >> columns;
tom = new int*[rows];
for (int i = 0; i < rows; i++) {
tom[i] = new int[columns];
}
for(int count1 = 0; count1 < rows; count1++)
{
for(int count2 = 0; count2 < columns; count2++)
{
cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
cin >> tom[count1][count2];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cout << tom[i][j] << endl;
}
}
return 0;
}
I'm trying to get get user input, which is stored in an array (eightBit[]), and then add that to a 2D array (board). The user is supposed to enter 8 numbers, for an example:
Byte 1: 1
Byte 2: 2
etc...
and the output is supposed to look like:
1 2 3 4
5 6 7 8
however this is the output I get:
8 8 8 8
8 8 8 8
Any idea why its repeating only the last numbered entered? Part of my code is below, any help would be appreciated.
cout << "Enter a pattern of eight bits:" << endl;
for(i = 0; i < 8; i++){
cout << "Byte " << i+1 << ": ";
cin >> eightBit[i];
}
int board[2][4];
for(i = 0; i<8; i++){
for(int j=0; j<2; j++){
for(int k=0; k<4; k++) {
board[j][k] = eightBit[i];
}
}
for(int j=0; j<2; j++)
{
for(int k=0; k<4; k++)
{
cout << board[j][k] << " ";
}
cout << endl;
}
That's because of your outer loop with i which is basically overwriting every element in your 2D array.
A solution would be to drop that outer loop entirely, like so:
int i = 0;
for(int j=0; j<2; j++) {
for(int k=0; k<4; k++) {
board[j][k] = eightBit[i++];
}
}
also you have bracket mismatch in your code snippet.
That's natural. In the second for when the i gets at last 8, then the board gets filled with the current i (i=8).
Try this, and next time be more careful with your code :).
#include <iostream>
using namespace std;
int eightBit[2][4];
int main()
{
cout << "Enter a pattern of eight bits:" << endl;
for(int i = 0; i <2; i++){
for (int j=0 ; j<4 ; ++j) {
cout << "Byte " << (j+1)+4*i << ": "; //4 = # of columns,i=row,j=column.
cin >> eightBit[i][j];
}
}
int board[2][4];
for(int i = 0; i <2; i++){
for (int j=0 ; j<4 ; ++j) {
board[i][j] = eightBit[i][j];
}
}
for(int i = 0; i <2; i++){
for (int j=0 ; j<4 ; ++j) {
cout << board[i][j] << " ";
}
cout << endl;
}
}