error: ‘imax’ was not declared in this scope - c++

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;
}

Related

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;
}

Matrix A find k -the number of positive elements

Enter the matrix A(NxM), output it. In each row of the matrix, find k -the number of positive elements. In the rows, all the elements after the kth are increased by the sum of the positive elements of this row. What's wrong with the program? The program incorrectly outputs the elements of the modified array.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
int n, m;
cout << "Введите количество строк: ";
cin >> n;
cout << "Введите количество столбцов: ";
cin >> m;
int A[10][10];
for (int i=0; i<n; i++)
for (int j=0; j<m; j++) {
cout<<"\nA["<<i<<"]["<<j<<"]=";
cin>>A[i][j];
}
cout << "\nМассив A:";
for (int i = 0; i < n; i++) {
cout<<"\n";
for (int j = 0; j < m; j++)
cout<<"\t"<<A[i][j];
}
cout<<endl;
int k;
for (int i = 0; i < n; i++)
{
k=0;
for (int j = 0; j < m; j++)
if(A[i][j]>0)
k++;
}
int sum;
for (int i=0; i<n; i++)
{
sum=0;
for (int j=0; j<m; j++)
if (A[i][j]>0)
sum+=A[i][j];
}
for (int i = k; i < n; i++)
for (int j = 0; j < m; j++)
A[i][j] = A[i][j] + sum;
cout << "Измененный массив A:";
for (int i = 0; i < n; i++) {
cout << "\n";
for (int j = 0; j < m; j++)
cout << "\t" << A[i][j];
}
}
You calculate k for each row, but after the loop k will only store the value of the last row. You need to store k for each row. Then in the loop:
for (int i = k; i < n; i++)
you skip the first k-1 rows, but you should add the sum in each row.
Change the last part of the code to:
int k;
std::vector<int> ks;
for (int i = 0; i < n; i++)
{
k=0;
for (int j = 0; j < m; j++)
if(A[i][j]>0)
k++;
ks.push_back(k);
}
int sum;
for (int i=0; i<n; i++)
{
sum=0;
for (int j=0; j<m; j++)
if (A[i][j]>0)
sum+=A[i][j];
}
for (int i = 0; i < n; i++)
for (int j = ks[i]; j < m; j++)
A[i][j] = A[i][j] + sum;

Find the index of the largest element

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.

multiply two negative numbers in c++

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];
}
}

How to read matrix and sum them up?

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;
}