C/C++
Помогите решить задачу по программированию C++
В файле содержатся числа. Сформировать из них матрицу, содержащую пять элементов в строке. Лишние числа отбросить. Вывести на печать матрицу в общепринятом виде и посчитать сумму элементов, содержащихся в предпоследней строке.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main() {
const auto n = 5U;
ifstream inp("numbers.txt");
vector<vector<int>> matrix;
if (inp.is_open()) {
vector<int> row;
int value;
while (inp >> value) {
row.push_back(value);
if (row.size() == n) {
matrix.push_back(row);
row.clear();
}
}
inp.close();
} else {
puts("File numbers.txt not found!");
}
if (!matrix.empty()) {
for (const auto& row : matrix) {
for (auto x : row) cout << setw(12) << x;
puts("\n");
}
auto last = matrix.size() - 1;
auto sum = 0LL;
for (auto x : matrix[last]) sum += x;
cout << "Sum: " << sum << '\n';
}
system("pause > nul");
}
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main() {
const auto n = 5U;
ifstream inp("numbers.txt");
vector<vector<int>> matrix;
if (inp.is_open()) {
vector<int> row;
int value;
while (inp >> value) {
row.push_back(value);
if (row.size() == n) {
matrix.push_back(row);
row.clear();
}
}
inp.close();
} else {
puts("File numbers.txt not found!");
}
if (!matrix.empty()) {
for (const auto& row : matrix) {
for (auto x : row) cout << setw(12) << x;
puts("\n");
}
auto last = matrix.size() - 1;
auto sum = 0LL;
for (auto x : matrix[last]) sum += x;
cout << "Sum: " << sum << '\n';
}
system("pause > nul");
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int lstr = 5;
const int lsto = 100;
int A[lsto][lstr];
int sto = 0;
const char file_name[] = "data.txt";
ifstream is;
is.open(file_name);
if(is.is_open())
{
while(!is.eof() && sto < lsto)
{
int x;
for(x = 0; x < lstr; x++)
{
if(is.eof()) break;
is >> A[sto][x];
}
if(x == lstr) sto++;
}
is.close();
}
for(int x = 0; x < sto; x++)
{
for(int y = 0; y < lstr; y++)
cout << A[x][y] << '\t';
cout << endl;
}
cout << endl;
int sum = 0;
for(int x = 0; x < lstr; x++)
sum += A[sto-2][x];
cout << "\tСумма предпоследней строки равна " << sum << endl;
return 0;
}
// пример файла с данными:

#include <fstream>
using namespace std;
int main()
{
const int lstr = 5;
const int lsto = 100;
int A[lsto][lstr];
int sto = 0;
const char file_name[] = "data.txt";
ifstream is;
is.open(file_name);
if(is.is_open())
{
while(!is.eof() && sto < lsto)
{
int x;
for(x = 0; x < lstr; x++)
{
if(is.eof()) break;
is >> A[sto][x];
}
if(x == lstr) sto++;
}
is.close();
}
for(int x = 0; x < sto; x++)
{
for(int y = 0; y < lstr; y++)
cout << A[x][y] << '\t';
cout << endl;
}
cout << endl;
int sum = 0;
for(int x = 0; x < lstr; x++)
sum += A[sto-2][x];
cout << "\tСумма предпоследней строки равна " << sum << endl;
return 0;
}
// пример файла с данными:

Похожие вопросы
- Помогите решить задачу по программированию на C++
- Помогите решить задачу по программированию
- Помогите решить задачу пожалуйста, в C++
- Можете помочь решить задачу по программированию.
- Задача по программированию C++
- Помогите решить задачу по C++!
- Программирование C++ Помогите решить задачу
- Помогите решить задачу на C++
- Помогите решить задачу, c++, функции
- Помогите решить задачу C++