I dont get a result in the console [c++] - c++

I've created a 2D array with length and width.
My minGW compiler says nothing to build.
Here's the code:
#include <iostream>
using namespace std;
const int rows = 5;
const int coloms = 5;
int Matrix[rows][coloms];
void ClearMatrix()
{
for(int i=0; i < rows; i++)
{
for(int j=0; j < coloms; j++)
{
Matrix[i][j] = 0;
}
}
}
void ShowMatrix()
{
for(int i; i < rows; i++)
{
for(int j; j < coloms; j++)
{
cout<< Matrix[i][j];
}
}
}
int main()
{
ClearMatrix();
ShowMatrix();
return 0;
}
What am I doing wrong here?

for(int i; i < rows; i++)
{
for(int j; j < coloms; j++)
{
cout<< Matrix[i][j];
}
}
You are accessing i and j which are uninitialized local variables. So, it's an undefined behavior.

Related

selection sort is not working properly it is not scanning the number which are at last of the array

#include<iostream>
using namespace std;
int main()
{
int arr[] = {40,20,14,20,55,14,22,45,22,447,441,224,421,2,14,1,9};
int size = sizeof(arr) / sizeof(int);
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
for (int i = 0; i < size; i++)
{
int Index_of_Min = i;
for (int j = i+1; j < size; j++)
{
if (arr[j] < arr[Index_of_Min])
{
Index_of_Min = j;
}
swap(arr[Index_of_Min], arr[i] );
}
}
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
in the above program when we run it the number 9, 224 are not getting sorted
i want to understand why the program is not working anyone who knows the solution then your help is much appreciated
and please explain me what was my mistake so i will not repeat it again.
Thank You
Saw the exact same error only a few days ago. Your loop is wrong, specifically the swap is in the wrong place. This is how it should look
for (int i = 0; i < size; i++)
{
int Index_of_Min = i;
for (int j = i+1; j < size; j++)
{
if (arr[j] < arr[Index_of_Min])
{
Index_of_Min = j;
}
}
swap(arr[Index_of_Min], arr[i] );
}
You do the swap after the inner loop has found the index of the minimum element, not while it is finding that index.

I am not sure why this vector transpose is not working

I want to tranpose a matrix using vector?
//I am trying to 'tranpose' a vector matrix, but it's not running
#include <iostream>
#include <vector>
using namespace std;
// this will tranpose matrix
int transpose(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}}}
// this will swap the matrix
int swap_vector(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m/2; j++) {
int temp = arr[i][j];
arr[i][j] = arr[i][j-1-i];
arr[i][j-1-i] = temp;
}}}
vector<vector<int>> arrh{{1,2,3,4},{5,6,7,8},
{9,10,11,12},
{13,14,15,16} };
int main() {
transpose(arrh);
swap_vector(arrh);
for (int i=0; i<arrh.size(); i++) {
for (int j=0; j<arrh[0].size(); j++) {
cout << arrh[i][j] << " ";}
cout << std::endl;}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
// this will tranpose matrix
int transpose(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = i; j < m; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
// this will swap the matrix
int swap_vector(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m/2; j++) {
int temp = arr[i][j];
arr[i][j] = arr[i][m-1-j];
arr[i][m-1-j] = temp;
}
}
}
vector<vector<int>> arrh{{1,2,3,4},{5,6,7,8},
{9,10,11,12},
{13,14,15,16} };
int main() {
transpose(arrh);
swap_vector(arrh);
for (int i=0; i<arrh.size(); i++) {
for (int j=0; j<arrh[0].size(); j++) {
cout << arrh[i][j] << " ";
}
cout << std::endl;
}
return 0;
}

(C++) How to imagine working of nested loops?

I have no idea what's going on how to imagine that, one more thing like in 3rd for loop there's a condition that k<j but its upper loop j is set to 0 and i is also 0 then as I think k=0 if this is right than 0<0 how's that can be valid????
void printing_subarrays(int *arr,int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
for(int k=i; k<j; k++){
cout<<arr[k]<<", ";
}cout<<endl;
}cout<<endl;
}
}
I don't think it makes much sense for us to explain what's happening, you need to see it for yourself.
Therefore I've added some output lines, which will show you how the values of the variables i, j and k evolve through the loops:
for(int i=0; i<n; i++){
cout<<"i=["<<i<<"]"<<endl;
for(int j=0; j<n; j++){
cout<<"i=["<<i<<"], j=["<<j<<"]"<<endl;
for(int k=i; k<j; k++){
cout<<"i=["<<i<<"], j=["<<j<<"], k=["<<k<<"]"<<endl;
cout<<arr[k]<<", ";
}
cout<<endl;
}
cout<<endl;
}
Did you try maybe running it? Then it should be apparent...
#include <iostream>
#include <vector>
void printing_subarrays(int *arr, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = i; k < j; k++) {
std::cout << arr[k] << ", ";
}
std::cout << "\n";
}
std::cout << std::endl;
}
}
int main() {
int n = 10;
std::vector<int> vec(n);
for (size_t i = 0; i < n; ++i) {
vec[i] = i;
}
printing_subarrays(vec.data(), n);
return 0;
}

Return a 2d array C++ from function [duplicate]

This question already has answers here:
Return a 2d array from a function
(10 answers)
Returning multidimensional array from function
(7 answers)
Closed last year.
I have a 2d array I want to transpose and return from a function. Below is my code and I get the following error
#include <iostream>
#include <bits/stdc++.h>
#include <chrono>
#include <fstream>
using namespace std;
#define R 550 // ROWS
#define C 550 // COLS
void generate_matrix(double (*mat)[C], int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
mat[i][j] = rand() % 100;
}
}
}
void print_matrix(double (*mat)[C], int n) {
printf("Printing Matrix:\n ");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << mat[i][j] <<" ";
}
}
}
void initialize_matrix(double (*mat)[C], int n) {
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
mat[i][j] = 0.0;
}
}
}
double** transpose_matrix(double (*mat)[C]) { // problem is here
double transpose[R][C];
for (int i = 0; i < R; ++i) {
for (int j = 0; j < R; ++j) {
transpose[j][i] = mat[i][j];
}
}
return transpose; // problem is here
}
int main() {
double A[R][C], B[R][C], result[R][C];
generate_matrix(A, C);
generate_matrix(B, C);
double** transposed_B = transpose_matrix(B);
print_matrix(transposed_B);
}
Error Message:
mat_mul_transpose.cpp: In function ‘double** transpose_matrix(double (*)[550])’:
mat_mul_transpose.cpp:46:12: error: cannot convert ‘double (*)[550]’ to ‘double**’ in return
return transpose;
How to fix this error?

Error in my code - Boolean Truth Table

I am currently working on a program that prints a 5 variable truth table. I am using a 2d array. My code currently produces the table, but says it is corrupt and "the stack around the variable "table" was corrupted. Any help?
#include <iostream>
using namespace std;
int main() {
bool table[5][32];
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
This is homework, so I would like to understand it, not just have an answer.
The index is wrong. Only table[0] to table[4] are available, so accessing table[5] to table[31] is illegal.
Try this:
#include <iostream>
using namespace std;
int main() {
bool table[32][5]; // swap 32 and 5
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
There is attempt to read out of bound values from array.
If you need 5x32 matrix Use code below:
for (int i = 0; i < 5; i++) { // 32-> 5
for (int j = 0; j < 32; j++) { // 5->32
If you need 32x5 matrix then replace code below:
bool table[32][5]; //it was table[5][32];