I am writing some code that writes a matrix with a 10x10 size and prints it using functions. What I need is a function that takes the sum of the entire matrix array. What I have done is that I tried to sum up each row and column separately and add the total together. The problem with mine is that I believe that it only prints out the sum of the row or the column.
This is the code I have:
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int ROW_SIZE = 10;
const int COLUMN_SIZE = 10;
void initialize(int [][10], int, int);
void display(int matrix[][10], int, int);
void sum(int matrix[][COLUMN_SIZE], int, int);
int main() {
int matrix [ROW_SIZE][COLUMN_SIZE];
initialize(matrix, ROW_SIZE, COLUMN_SIZE);
display(matrix, ROW_SIZE,COLUMN_SIZE);
sum(matrix, ROW_SIZE,COLUMN_SIZE);
return 0;
}
void initialize(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
for (int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
matrix[i][j] = 1 + rand() % 99;
}
}
}
void display(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
}
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum_row;
int sum_col;
for(int i = 0; i < ROW_SIZE; i++){
int sum_row = 0;
for(int j = 0; j < COLUMN_SIZE; j++){
sum_row = sum_row + matrix[i][j];
}
}
for(int i = 0; i < ROW_SIZE; i++){
int sum_col = 0;
for(int j = 0; j < COLUMN_SIZE; j++){
sum_col = sum_col + matrix[i][j];
}
}
int sum = sum_row + sum_col;
cout<<"The sum of the matrix is "<<sum<< endl;
}
What you have done is sum of last row + last column. This should be enough:
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum = 0;
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
sum += matrix[i][j];
}
}
cout<<"The sum of the matrix is "<<sum<< endl;
}
few early problems in your code
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum_row; // uninitialized make it equal to 0
int sum_col; // uninitialized make it equal to 0
for(int i = 0; i < ROW_SIZE; i++){
int sum_row = 0; // This is local to block and masks the sum_row above
for(int j = 0; j < COLUMN_SIZE; j++){
sum_row = sum_row + matrix[i][j];
}
}
for(int i = 0; i < ROW_SIZE; i++){
int sum_col = 0;
for(int j = 0; j < COLUMN_SIZE; j++){
sum_col = sum_col + matrix[i][j]; //Iterating over rows again matrix[j][i] may be
}
}
int sum = sum_row + sum_col;
cout<<"The sum of the matrix is "<<sum<< endl;
}
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum_row = 0;
int sum_col = 0;
for(int i = 0; i < ROW_SIZE; i++)
for(int j = 0; j < COLUMN_SIZE; j++)
sum_row += matrix[i][j];
for(int i = 0; i < ROW_SIZE; i++)
for(int j = 0; j < COLUMN_SIZE; j++)
sum_col += matrix[j][i];
int sum = sum_row + sum_col;
cout<<"The sum of the matrix is "<<sum<< endl;
}
Now of course the question and logic doesn't seem to co-exist together and if sum of entire matrix means sum of all elements which is what it sounds like it can be achieved with the one here
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum = 0;
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
sum += matrix[i][j];
}
}
cout<<"The sum of the matrix is "<<sum<< endl;
}
You're doing something wrong in terms of naming. You have constants called ROW_SIZE and COLUMN_SIZE and then you have parameters with the same names to functions like sum(). This is confusing for something reading your code. Call your parameters something else, like rows and columns.
Here is an alternative to the proposed solutions using std::accumulate().
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 3;
void sum(int matrix[][COLUMN_SIZE], int const rows, int const cols)
{
int sum = std::accumulate(&matrix[0][0], &matrix[rows-1][cols], 0);
std::cout<<"The sum of the matrix is "<<sum<< std::endl;
}
int main()
{
int m[ROW_SIZE][COLUMN_SIZE] {{1,2,3}, {4,5,6}, {7,8,9}};
sum(m, ROW_SIZE, COLUMN_SIZE);
}
std::accumulate() takes the iterators that delimit a range. Those are the iterators to the first element and the one past the last element of the range. In this case, these are &matrix[0][0] and &matrix[row-1][col] (since &matrix[row-1][col-1] is the last element of the matrix). There are other ways to put that, such as the following:
auto begin = std::begin(matrix[0]);
int sum = std::accumulate(begin, begin + rows * cols, 0);
Related
The problem with my code is that it is not identifying my function, I am not sure if the function is incorrect or written with the wrong syntax. What I have tried is to create a new array for the location of the largest index but it doesn't seem to work.
#include <iostream>
#include <iomanip>
using namespace std;
void locateLargest(const double a[][4], int location[]);
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
int main(){
int location [ROW_SIZE][COLUMN_SIZE];
double matrix [ROW_SIZE][COLUMN_SIZE];
double input;
cout<<"Enter the array: "<< endl;
for (int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cin>>input;
matrix[i][j] = input;
}
}
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
locateLargest(matrix, location)
}
You can keep track of the max value's indices while iterating through the matrix.
void max_idx(const double (&arr)[RS][CS]) {
double curr_max = arr[0][0];
size_t max_i = 0, max_j = 0;
for (size_t i = 0; i < RS; ++i) {
for (size_t j = 0; j < CS; ++j) {
if (curr_max < arr[i][j]) {
curr_max = arr[i][j];
max_i = i;
max_j = j;
}
}
}
cout << "Largest value is at (i=" << max_i << ", j=" << max_j << ")\n";
}
Demo
First of all, you have to make sure that your code is consistent : in the prototype of your locateLargest function, location is a one-dimensional array but in your main() function it is a two-dimensional one.
This is how I would write this :
#include <iostream>
#include <iomanip>
using namespace std;
void locateLargest(double** a, int* location);
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
int main()
{
int location [2];
double* matrix [ROW_SIZE];
for(int s= 0; s< ROW_SIZE; s++)
{
matrix[s]= new double[COLUMN_SIZE];
}
double input;
cout<<"Enter the array: "<< endl;
for (int i = 0; i < ROW_SIZE; i++)
{
for(int j = 0; j < COLUMN_SIZE; j++)
{
cin>>input;
matrix[i][j] = input;
}
}
for(int i = 0; i < ROW_SIZE; i++)
{
for(int j = 0; j < COLUMN_SIZE; j++)
{
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
locateLargest(matrix, location);
}
void locateLargest(double** a, int* location)
{
int i, j;
double maxVal= a[0][0]; location[0]= location[1]= 0;
for(i = 0;i < ROW_SIZE; i++)
{
for(j = 0; j < COLUMN_SIZE; j++)
{
if(maxVal < a[i][j])
{
location[0] = i;
location[1]= j;
maxVal= a[i][j];
}
}
}
cout << "The location of the largest element is at ("<< location[0] << " , "<<
location[1] <<" ) . it is : "<< maxVal<<endl;
}
max represents the maximum value of your matrix's elements, you first set it to be equal to the first element and then compare it to each element of the matrix. Each time you find an element that is larger than max, you assign his value to max and his position to location and at the end of the iterations, you have the largest value and his location.
when I tried to multiple two negative numbers the value it is zero in c++,
for example -5 * -3
the result is zero,
why?
this is my code
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void Multiply(const int v_arr[], const int m_arr[][3], int signed
o_arr[], int size)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
o_arr[i] = 0;
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
}
//End your code here
}
int main()
{
int n;
cin >> n;
int v_array[n];
int m_array[n][3];
int signed o_array[3];
for (int i = 0; i < n; i++) {
cin >> v_array[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> m_array[i][j];
}
}
//fuction
Multiply(v_array, m_array, o_array, n);
for (int j = 0; j < 3; j++) {
cout << o_array[j] << " ";
}
return 0;
}
how to fix it to get the correct result?
the input is
2
2 -3
2 -3
2 -4
Your issue is here:
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
You access elements at indices 0, 1 and 2 in v_arr, but it only has 2 elements. That's Undefined Behaviour.
Assuming this is matrix*vector multiplication code, it should look like this (untested):
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
Also, your loop based on j is useless. You can remove it:
void Multiply(const int v_arr[], const int m_arr[][3], int signed o_arr[], int size)
{
for(int k = 0; k < 3; k++) { //initialize output array
o_arr[k] = 0;
}
for (int i = 0; i < size; i++) {
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
}
I am relatively new to C++ and I have an assignment that is making perform mathematical operations with matrices. I was able to complete the operations but I can't seem to display the original matrices I inputted into the output with out doing a "cout" command for each number. I'm trying to display the original matrices before the answer to the 5 questions.
This is what I have done:
#include <stdio.h>
#include <iostream>
using namespace std;
void matrix(int A[3][3], int B[3][3], int result[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
result[i][j] += (A[i][k] * B[k][j]);
}
}
}
void scalar(int A[3][3], int num, int result[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result[i][j] = A[i][j] * num;
}
}
}
void display(int result[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
cout << result[i][j] << ",";
cout << endl;
;
}
}
int main()
{
//inputting matrix values
int A[3][3]={{13,9,7},{8,7,4},{6,4,0}};
int B[3][3]={{1,3,5},{6,2,-3},{2,0,4}};
int C[3][3]={{1,0,0},{0,1,0},{0,0,1}};
int result[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 1
matrix(A,B,result);
cout<<"A * B ="<<endl;
display(result);
int result0[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 2
matrix(B,A,result0);
cout<<"B * A ="<<endl;
display(result0);
int result1[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 3
matrix(C,B,result1);
cout<<"C * B ="<<endl;
display(result1);
int result2[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 4
scalar(A,2,result2);
cout<<"2 * A ="<<endl;
display(result2);
int result3[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 5
scalar(B,-4,result3);
cout<<"-4 * B ="<<endl;
display(result3);
return 0;
}
I know Bucket sort is has a lot of examples everywhere, so I tried to implement this so it can take huge random numbers with no luck
void Bucket_sort(int arr[], int max){
const int maxsize = max;
int bucket_list = new int [maxsize+1];
int length = sozeof(bucket_list) / sizeof(bucket[0]);
for(int i = 0; i <max;i++){
bucket_list[i] = 0; //fill with zeros
}
for (unsigned int i = 0; i <length; i++){
bucket_list[arr[i]]++;
}
int position = 0;
for (unsigned int i = 0 i < length; i++){
for(int k = 0; k<bucket_list[i];k++){
arr[position++] = i;
}
}
}
int main() {
int max = 50000
int arr[max];
for (int i = 0; i < max; i++){
arr[i] = rand() % 50000;
}
cout<<"Here are the numbers before Bucker Sort"<<endl;
for (int j = 0; j < max; j++){
cout<<arr[j];
}
Bucket_sort(arr,max);
for (int k = 0; k<max; k++){
cout<<arr[k];
}
}
some how I can't get it working, it will just out put the same order (unsorted)
I did find some same questions as mine, but none of them helped, here is one
https://stackoverflow.com/questions/20037176/c-bucket-sort-putting-integers-into-buckets
This line:
bucket_list = 0; //fill with zeros
this is changing your pointer, not filling with zeros. You should use
bucket_list[i] = 0; //fill with zeros
Edit: There are a lot more compiler issues with your code. Once you have those sorted out, the calculation of length is still wrong. You can't use the sizeof dividing trick, because bucket_list isn't an array. Replace
int length = sozeof(bucket_list) / sizeof(bucket[0]);
with
int length = maxsize
or just don't use length at all (you already have maxsize).
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void Bucket_sort(int arr[], int max){
int maxsize = max;
int *bucket_list = new int[maxsize+1];
// int length = sozeof(bucket_list) / sizeof(bucket[0]);
int length = maxsize;
for(int i = 0; i <max;i++){
bucket_list[i] = 0; //fill with zeros
}
for (unsigned int i = 0; i <length; i++){
bucket_list[arr[i]]++;
}
int position = 0;
for (unsigned int i = 0 ; i < length ; i++){
for(int k = 0; k<bucket_list[i];k++){
arr[position++] = i;
}
}
}
int main() {
int max = 50;
int arr[max];
for (int i = 0; i < max; i++){
arr[i] = rand()%50;
}
cout<<"Here are the numbers before Bucker Sort"<<endl;
for (int j = 0; j < max; j++){
cout<<arr[j];
}
Bucket_sort(arr,max);
for (int k = 0; k<max; k++){
cout<<arr[k];
}
getch();
return 0;
}
Passing matrix as a pointer to pointer to function not working.
#include <stdio.h>
void printMatrix(int **matrix, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
printf("%d ", matrix[i][j]);
printf("\r\n");
}
}
void printM (size_t row, size_t col, int matrix[3][4])
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
printf("%d ", matrix[i][j]);
printf("\r\n");
}
}
int main()
{
int M[3][4];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
M[i][j] = 4*i+j;
printM(3, 4, M);
int *row = *M;
printMatrix(&row, 3, 4); //not working
}
Function printM works, but I would like to know how to use pointer to pointer correctly, thanks for help.
First of all, thank-you for this question. It is a good review of how C does multi-dimensional arrays. Also, it is OK to do double pointers. Remember an array reference is equivalent to a pointer, such as: a[0] and *a both refer to the first element of int a[12]; where *a is the de-referencing of pointer a. And so, &M is a the address of the pointer M when M is declared as int M[3][4];
I modified your code by adding a few comments for clarity and so that it would run in Eclipse using a C compiler from Microsoft, specifically, int declarations where moved out of the for statements. Other than that it is the same as what you originally wrote with a change to the printMatrix declaration and how it is invoked.
Hope this helps, please ask if more questions...
#include <stdio.h>
void printMatrix(int (*matrix)[3][4], int row, int col)
{
int i, j;
// point t so that when de-referenced it is at
// the matrices first element
int *t = (*matrix)[0];
printf("\n");
for (i = 0; i < row; i++)
{
// in C matrices are stored in Row Major form, so
// per K&R just sequentially loop thru (*t)[12]
for (j = 0; j < col; j++) {printf("%d ", *t++);}
printf("\r\n");
}
} // end printMatrix
void printM (size_t row, size_t col, int matrix[3][4])
{
int i, j;
printf("\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++) {printf("%d ", matrix[i][j]);}
// new line for next row
printf("\r\n");
}
}
int main()
{
int i,j;
// define a matrix with 3 rows and 4 columns
int M[3][4];
// fill-in the matrix with values
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
M[i][j] = 4*i + j;
// print the three rows and four columns of M
printM(3, 4, M);
printMatrix(&M, 3, 4); // Also Works
} // end main
void printMatrix(int *matrix, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
printf("%d ", *(matrix+(i*col)+j);
printf("\r\n");
}
}
Don't do a double pointer.