To begin to address the tasks, proposed in this article, you need to know that is two-dimensional (multidimensional) arrays . Tasks are arranged from simple to complex. So that, If you're new to programming, start with the first, to better deal with the other.
1. Declare a two-dimensional array, fill integers and show on the screen.
2) Объявить двумерный массив и заполнить его построчно с клавиатуры. После заполнения – показать заполненную матрицу на экран и посчитать сумму элементов отдельно в каждом столбце и каждой строке.
3) Fill two-dimensional array of random numbers 10 to 100. Calculate the sum of the elements separately in each line and determine the line number, in which this sum is maximal.
Your questions, которые возникли при решении задач, leave comments on this article.
In the hammer throw competes n sportcmenov. Each of them made m rolls. The winner of the competition the athlete, whose best result is maximum on all rolls. In this way, program to find the maximum value of the element in the array, and its indexes (ie athlete number and the number of attempts).
Input data
The program takes as input two integers n and m, is the number of rows and columns in an array. Further, in the input stream is n rows by m numbers, It is an array of elements.
Output
The program prints the value of the maximum element, then the line number and column number, in which it occurs. If the array has several maximal elements, it is necessary to bring the minimum line number, wherein an element is found, and if this line of several elements, it is necessary to bring the minimum column number. Remember, that all rows and columns are numbered from 0.
examples
input data
3 3
3 1 2
1 3 4
3 3 3
output
4
1 2
And what do you expect?
That someone will decide for you your homework while you walk widely?
So for such a pleasure to pay money!
#include “stdafx.h”
#include
#include
#include
#include // setw(indent)
using namespace std;
int main() {
setlocale(0, “”);
srand(time(NULL));
const int ROWSIZE = 3;
const int COLSIZE = 3;
int arr[ROWSIZE][COLSIZE] = {};
// filling the array with keyboard
/*
for (int rowNum = 0; rowNum < ROWSIZE; rowNum ) {
for (int colNum = 0; Coin < COLSIZE; Coin ++) {
cout << "Пожалуйста, enter the initial values of the array: ";
cout << rowNum + 1 << "-я строка ";
cout << Coin + 1 <> arr[rowNum][Coin];
}
}
*/
// Fill the array with random numbers
for (int rowNum = 0; rowNum < ROWSIZE; rowNum ) {
cout << "Строка " << rowNum + 1 << ": ";
for (int colNum = 0; Coin < COLSIZE; Coin ++) {
arr[rowNum][Coin] = 1 + rand() % 9;
cout << setw(2) << arr[rowNum][Coin];
}
cout << endl;
}
// We find the maximum number of, and if there are several we find the very first row and column, where it occurs
int row[ROWSIZE]{};
int col[COLSIZE]{};
int temp = 0;
int temp1 = 0;
for (int r = 0; r < ROWSIZE; r++) {
for (int c = 0; c < COLSIZE; c++) {
if (col[0] < arr[c][r]) {
col[0] = arr[c][r];
temp = r;
temp1 = c;
}
else c;
}
}
cout << "Максимальное: " << col[0] << " COL: " << temp1 + 1 << " ROW: " << temp + 1 << endl;
system("pause");
return 0;
};
#include
#include
#include
#include
#include
int main ()
{
setlocale(LC_ALL, “rus”);
srand(time(NULL));
int N, M;
printf (“Enter the number of athletes: “);
scanf (“%d”, &N);
printf (“Enter the number of attempts: “);
scanf (“%d”, &M);
int **A = new int *[N];
for (int count = 0; count < N; count )
A[count] = new int [N];
int nMax = 0, iMax, jmax;
int i, j;
printf ("\nТаблица с результатами: \n");
for (i = 0; i < N; i )
{
printf ("Спортсмен №%d: ", i+1);
for (j = 0; j < M; j )
{
A[i][j] = rand()%25 + 1;
printf ("%3d ", A[i][j]);
if(nMax == A[i][j])
{
if(i < iMax && j < jmax)
nMax = A[i][j];
else
nMax != A[i][j];
}
if(nMax < A[i][j])
{
nMax = A[i][j];
iMax = i;
jMax = j;
}
}
printf ("\n");
}
printf ("\nЛучший результат – %d y sporstmen №% d in an attempt №% d!", nMax, iMax + 1, jmax + 1);
for (int count = 0; count < N; count )
delete [] A[count];
return 0;
}
int main()
{
setlocale(LC_ALL, “rus”);
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
srand(time(NULL));
const int n = 5, m = 7;
int MM[n][m]{}, max = 0,r,s,R,S;
for (int i = 0; i < n; i )
{
for (int j = 0; j < m; j )
{
MM[i][j] = 15 + rand() % 55;
cout << MM[i][j] << "|";
}
cout << endl;
}
for (int i = 0; i < n; i )
{
for (int j = 0; j max)
{
max = MM[i][j];
r = i;
s = j;
}
if (max==MM[i][j] && i != r && j != s)
{
cout << "Opapa! In U.S 2 Pobeditelya!\n";
R = i;
S = j;
cout << "Pobedu delyat uchastnik #" << r + 1 << " i uchastnik #" << R + 1 << " S rezultatom " << max << " m!\n Pozdravlyaem!";
return 0;
}
}
}
cout << "Pobedil uchastnik #" << r+1 << " s " << s+1 << " poputki, S rezultatom " << max << " m!\n Pozdravlyaem!";
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
setlocale(LC_ALL, “Rus”);
const int N = 10, M = 15;
int arr[N][M], minn=999999, countmx=0, max=-1, digitsr, digitsl;
bool flag = false;
for (int i = 0; i < N; i )
{
for (int j = 0; j < M; j )
{
arr[i][j] = rand() % 150;
cout << "[" << i << "]" << "[" << j << "] = " << arr[i][j] << endl;
if (arr[i][j] == max)
{
countmx += 1;
if (!flag)
{
by j =;
flag = true;
}
if (j max)
{
max = arr[i][j];
digitsr = i;
digitsl = j;
}
}
}
if (countmx != 0)
cout << "Мин. column number = " << minn;
else
{
cout << "Максимальное значение элемента = " << max << endl;
cout << "Номер строки = " << digitsr << endl;
cout << "Номер столбца = " << digitsl;
}
return (0);
}
oops, I took a certain value of N and M
cout << "[" << i << "]" << "[" << j << "] = " << arr[i][j] << endl
^ This should not be, I forgot to remove
int ourMatrix[MatrixSize][MatrixSize] = {}
why there “{}”?
Explain, please!!! why the sum of the columns displayed wrong??
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “rus”);
srand(time(NULL));
const int RowCount = 10;
int array[RowCount][RowCount] = {};
int SumRow[RowCount] = {}, SumColumn[RowCount] = {};
for (int i = 0; i < RowCount; i ) {
for (int j = 0; j < RowCount; j ) {
array[i][j] = i*j;
cout << setw(4) << array[i][j] << " ";
SumRow[i] += array[i][j];
SumColumn[i] += array[j][i];
}
cout << endl;
}
for (int i = 0; i < 10; i ) {
cout << "Сумма в " << i + 1 << " string = " << SumRow[i] << " ";
cout << "Сумма в " << i + 1 << " column = " << SumColumn[i] << endl;
}
system("pause");
return 0;
}
String “SumColumn[i] += array[j][i];” adds more blank cell array with random numbers.
I dont know, Is more topic is relevant. last comment very old. But I'm interested in the same question, that prediduschemu comments.
the solution of the problem 2 It works only with constant and only with the troika. With the rest of the sum of the numbers wrong. I would like an explanation, if possible. And the decision to randomly entering and input array size keyboard
Problem №2 with randomly input.
#include
#include
#include
#include
int main ()
{
setlocale(LC_ALL, “rus”);
srand(time(NULL));
int N, M, i, j;
printf (“Введите количество строк двумерного массива: “);
scanf (“%d”, &N);
printf (“Введите количество столбцов двумерного массива: “);
scanf (“%d”, &M);
int **A = new int *[N];
for (int count = 0; count < N; count )
A[count] = new int [N];
printf ("\nИсходный двухмерный массив: \n");
for (i = 0; i < N; i )
{
for (j = 0; j < N; j )
{
A[i][j] = rand()%99+ 1;
printf ("%4d ", A[i][j]);
}
printf ("\n");
}
printf ("\nСумма элементов по строкам\n");
int sumi;
for (i = 0; i < N; i )
{
sumi = 0;
for (j = 0; j < N; j )
{
sumi += A[i][j];
}
printf ("%d-я строка: %d \n", i, sumi);
}
printf ("\nСумма элементов по столбцам\n");
int sumj;
for (j = 0; j < N; j )
{
sumj = 0;
for (i = 0; i < N; i )
{
sumj += A[i][j];
}
printf ("%d-й столбец: %d \n", j, sumj);
}
for (int count = 0; count < N; count )
delete [] A[count];
return 0;
}
Problem №2 array with input values from the keyboard
#include
int const N = 4;
int main ()
{
setlocale(LC_ALL, “rus”);
int A[N][N];
int i, j;
printf (“\nÇàïîëíèòå äâóìåðíûé ìàññèâ% dx% d ÷ ÷ èñëàìè ïîñòðî II: \n”, N, N);
for (i = 0; i < N; i )
{
for (j = 0; j < N; j )
{
printf ("A[%d][%d] = ", i, j);
scanf ("%d", &A[i][j]);
}
}
printf ("\nÈñõîäíûé äâóìåðíûé ìàññèâ: \n");
for (i = 0; i < N; i )
{
for (j = 0; j < N; j )
{
printf ("%2d", A[i][j]);
}
printf ("\n");
}
printf ("\nÑóììà ýëåìåíòîâ ïî ñòðîêàì\n");
int sumi;
for (i = 0; i < N; i )
{
sumi = 0;
for (j = 0; j < N; j )
{
sumi += A[i][j];
}
printf ("%d-ÿ ñòðîêà: %d \n", i, sumi);
}
printf ("\nÑóììà ýëåìåíòîâ ïî ñòîëáöàì\n");
int sumj;
for (j = 0; j < N; j )
{
sumj = 0;
for (i = 0; i < N; i )
{
sumj += A[i][j];
}
printf ("%d-é ñòîëáåö: %d \n", j, sumj);
}
return 0;
}
Thank you))
ooh, in randomness did clerical to the columns cycles, falsified
int main ()
{
setlocale(LC_ALL, “rus”);
srand(time(NULL));
int N, M, i, j;
printf (“Введите количество строк двумерного массива: “);
scanf (“%d”, &N);
printf (“Введите количество столбцов двумерного массива: “);
scanf (“%d”, &M);
int **A = new int *[N];
for (int count = 0; count < N; count )
A[count] = new int [N];
printf ("\nИсходный двухмерный массив: \n");
for (i = 0; i < N; i )
{
for (j = 0; j < M; j )
{
A[i][j] = rand()%99+ 1;
printf ("%4d ", A[i][j]);
}
printf ("\n");
}
printf ("\nСумма элементов по строкам\n");
int sumi;
for (i = 0; i < N; i )
{
sumi = 0;
for (j = 0; j < M; j )
{
sumi += A[i][j];
}
printf ("%d-я строка: %d \n", i, sumi);
}
printf ("\nСумма элементов по столбцам\n");
int sumj;
for (j = 0; j < N; j )
{
sumj = 0;
for (i = 0; i < M; i )
{
sumj += A[i][j];
}
printf ("%d-й столбец: %d \n", j, sumj);
}
for (int count = 0; count < N; count )
delete [] A[count];
return 0;
}
Please tell me why it is wrong and how to fix it
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “rus”);
int clove = 0;
int stolb=0;
int more[stroke][stolb]={};
int strokSUM = 0;
int stolbSUM=0;
cout <> stroke;
cout <> stolb;
cout << endl;
cout << "==== Заполните матрицу " << stroke << "x" << stolb << " ==== " << endl;
cout << endl;
for (int i=0; i<stroke; i ){
for (int j=0; j<stolb; j ){
cout << i+1 << "-я строка ";
cout << j+1 <> but[stroke][stolb];
}
cout << endl;
}
for (int i = 0; i < stroke; i ){
cout << " | ";
for (int j = 0; j < stolb; j ){
cout << setw(4) << but[i][j] << " ";
Strokshun[i] +but =[i][j];
stolbSUM[i] +but =[j][i];
}
cout << "|" << endl;
}
return 0;
}
int clove = 0;
int stolb=0;
1. The rows and columns should have a constant value.
const int strok and const int stolb should be.
2. How are you going to create an array?) by you 0 rows and columns.
cout << "Строка, сумма элементов которой максимальна: " << numMaxRow + 1;
Why numMaxRow + 1 (then there are + 1)?
Without +1 It does not work correctly, and from +1 fine. What role is played here +1 ?
working correctly. In the array element numbering starts at 0.
That is. then, what we consider the first row in the array has an index 0. And for better perception we add +1. That is. the output line index 0 It will be numbered 1 when displayed in the console.
It seems right and clearly explained))
#include
using namespace std;
int main() {
int arr[100][100];
int n, m, max = 0, sti, stj;
cin >> n >> m;
for (int i = 0; i < n; i ) {
for (int j = 0; j > arr[i][j];
}
}
for (int i = 0; i < n; i ) {
for (int j = 0; j max) {
max = arr[i][j];
}
sti = i + 1;
stj = j + 1;
}
}
cout << "Max = " << max << endl << sti << " " << stj;
}