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.
Related
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;
}
How would I swap 2d array columns in places, but the swap has to happen between the columns that contain the smallest and biggest element in the 2d array. Any help on this would be appreciated.
#include <iostream>
#include <ctime>
#include <cstdlib>
#define rows 4
#define cols 4
using namespace std;
int main () {
srand(time(0));
int i=0, j=0,n,low,high,lowpos,highpos,a,b;
int arr[rows][cols];
for (i=0; i<rows; i++)
for (j=0; j<cols; j++)
arr[i][j] = 1 + (rand() % 200);
cout << "Random 2d generated array:" << endl;
for (i=0; i<rows; i++){
for (j=0; j<cols; j++)
cout << arr[i][j] << " ";
cout << endl;
}
high=arr[0][0];
low=arr[0][0];
for(i=0;i<rows;++i)
{
for(j=0;j<cols;++j)
{
if(arr[i][j]>high)
high=arr[i][j];
else if(arr[i][j]<low)
low=arr[i][j];
}
}
cout<<"\nBiggest element:"<<high<<"\nSmallest:"<<low<<"\n";
}
I decided to stay in c++11 standard. Of course swap helps do the trick.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#define rows 4
#define cols 4
using Arr = int[rows][cols];
std::pair<int, int> high_low_ids(const Arr arr, int nrow = rows, int ncol = cols) {
int highpos = 0, lowpos = 0;
int high = arr[0][0];
int low = arr[0][0];
for(int i = 0; i < nrow; ++i)
for(int j = 0; j < ncol; ++j)
if(arr[i][j] > high) {
highpos = j;
high=arr[i][j];
}
else if(arr[i][j] < low) {
lowpos = j;
low=arr[i][j];
}
std::cout << "\nBiggest element:" <<high<< "\nSmallest:" << low << "\n";
return {highpos, lowpos};
}
void rows_swap(Arr table2d, int nrow = rows){
auto p = high_low_ids(table2d);
const int high_idx = p.first;
const int low_idx = p.second;
if (high_idx == low_idx)
return;
for (int i = 0; i < nrow; ++i) {
std::swap(table2d[i][high_idx], table2d[i][low_idx]);
}
}
void print_arr(Arr arr, int nrow = rows, int ncol = cols) {
for (int i = 0; i < nrow; i++) {
for (int j = 0; j < ncol; j++)
std::cout << std::left << std::setw(6) << arr[i][j];
std::cout << std::endl;
}
}
int main () {
srand(time(0));
Arr arr;
for (int i = 0; i < rows; ++i)
for (int j=0; j<cols; ++j)
arr[i][j] = 1 + (rand() % 200);
std::cout << "Random 2d generated array:\n";
print_arr(arr);
rows_swap(arr);
std::cout << "Swapped array:\n";
print_arr(arr);
return 0;
}
PS. please avoid using namespace std.
I'm getting the (main.cpp:15:13: error: ‘imax’ was not declared in this scope) error while compiling this code.
Here's the task:
Given k number and kxk z matrix. Get new b[i] vector which elements are taken from the product of the elements preceding the last max element
#include <iostream>
using namespace std;
int indexMax (int z[20][20], int k){
for (int i = 0; i < k; i++){
int max = z[i][0];
int imax = 0;
for(int j = 0; j < k; j++){
if (z[i][j]>=max)
max=z[i][j];
imax=j;
}
}
return imax;
}
int product(int z[20][20], int k){
int imax=indexMax(z,k);
int i;
int P=1;
for(int j = 0; j < imax-1; j++){
P*=z[i][j];
}
return P;
}
int main()
{
int z[20][20],b[20],k;
cout << "k=";
cin >> k;
cout << "Matrix " << k << "x" << k << ":\n";
for (int i = 0; i < k; i++){
for (int j = 0; j < k; j++){
cin >> z[i][j];
}
}
cout << "b[i]:\n";
for (int i = 0; i < k; i++){
b[i] = product(z, k);
cout << b[i] << " ";
}
return 0;
}
imax is scoped by the outer for loop and hence not accessible outside it. The fix is to move the declaration outside the for loop (I initialized it here in case k == 0 but leaving the assignment imax = 0 in the loop as to not change behavior):
int indexMax (int z[20][20], int k){
int imax = 0;
for (int i = 0; i < k; i++){
int max = z[i][0];
imax = 0;
for(int j = 0; j < k; j++){
if (z[i][j]>=max)
max=z[i][j];
imax=j;
}
}
return imax;
}
What's wrong with my code
I Have to add all the edge values of the 2D Array.
The Function is not returning what I want. Its returning garbage value.
Program Req Output : Add all the edges value of the 2D Array and return its answer.
You are not allowed to use Vector etc.
Here is my Code :
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
int k = 0;
int l = 0;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= cols; j++)
{
if (i == 1 || i == rows || j == 1 || j == cols)
{
sum = arr[k][l];
totalOfEdges = sum + totalOfEdges;
}
l++;
}
k++;
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
My Output :
Leaving the Correct Code in Case Any one need it .
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (i == 0 || i == (rows-1) || j == 0 || j == (cols-1))
{
sum = arr[i][j];
totalOfEdges = sum + totalOfEdges;
}
}
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
cout << "--------" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
I wanted to do a coding where it reads a 4x4 matrices and sum them up. I dont know where I did wrong. My result is it keeps on asking to enter the elements. I just wanted a 4x4. Can anyone help me?
#include <iostream>
using namespace std;
const int SIZE = 4;
double sumColumn(const double m[][SIZE], int rowSize, int columnIndex)
{
int sum = 0;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
sum = sum + m[i][j];
}
}
return sum;
}
int main()
{
double m[SIZE][SIZE], sum = 0;
cout << "Enter the elements of the matrix" << endl;
for (int i = 0; i<SIZE; i++)
for (int j = 0; j<SIZE; j++)
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
cout << sum << endl;
return 0;
}
A good practice is using curly braces even if the content of the "for" has one line, but in your case must be in the next way
for (int i = 0; i<SIZE; i++) {
for (int j = 0; j<SIZE; j++) {
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
}
}
Greetings
The complete code is:
#include <iostream>
using namespace std;
const int SIZE = 4;
double sumColumn(const double m[][SIZE], int rowSize, int columnIndex)
{
int sum = 0;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
sum = sum + m[i][j];
}
}
return sum;
}
int main()
{
double m[SIZE][SIZE], sum = 0;
cout << "Enter the elements of the matrix" << endl;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
}
}
cout << sum << endl;
return 0;
}