I have following program. with Input 3 5
3 rows
5 growth of numbers
The output should be:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
But my program gives:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Here is what I have tried so far
int main() {
int n, m, c = 0;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
}
What I am doing wrong or missing?
About the spaces: Can't find reason for such behavior(first spaces are ignored), displayed on screenshot. Tried to run in different IDE's with different compilers and had such problem only in testing system.
Hi try to use tab instead.
#include <iostream>
using namespace std;
int main() {
int n, m, c = 0;
cin >> n >> m;
int *a = new int[n * m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i * n + j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << "\t" << a[i * n + j];
cout << endl;
}
delete[] a;
return 0;
}
Can't remember how I solved this problem in secondary school, but with n less than m, the following code works:
#include <iostream>
using namespace std;
void nextij(long n,long m,long& i,long& j) {
if (i==n-1) { //bottom row
if (j<n-1) { //in the left square
j = i+j+1;
i = 0;
}
else { //out of the left square
i = j-(n-1)+1;
j = m-1;
}
}
else { //other rows
if (j==0) { //left most column
j = i+1;
i = 0;
}
else { //other columns
i++;
j--;
}
}
}
int main() {
long n = 3;
long m = 5;
long a[3][5];
long i = 0;
long j = 0;
long c = 1;
while (c<=n*m) {
a[i][j] = c;
nextij(n,m,i,j);
c++;
}
for (i=0; i<n; i++) {
for (j=0; j<m; j++)
cout <<a[i][j] <<" ";
cout <<endl;
}
}
/*
output:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
*/
Related
How to read the content of matrix from file to again in the matrix. so I can use it for updating some value in it and again copy this in the matrix ...
In this first, I check that file is empty or not if it is empty then I write a matrix with all values zero, if it is not empty then I read the value from the file and put it into the matrix and then I make some update in it and write it back to the file with updating values...but I unable to read the matrix from file.
Thanks in advance (beginner).
please help me, I tried it so much time but I unable to do it, It is part of my project.
#include <bits/stdc++.h>
using namespace std;
class block{
public:
void inc(int arr[][10])
{ int k;
cout<<"by how much value do you want to increment the array's values"<<endl;
cin>>k;
for(int i=0 ; i<10; i++)
for(int j =0 ; j<10 ; j++)
arr[i][j] = arr[i][j] + k;
for(int i=0 ; i<10; i++)
{
for(int j =0 ; j<10 ; j++)
{
cout<<arr[i][j];
}
cout<<endl;
}
}
void details(int arr[][10])
{
int m1 = 10;
int n1 = 10;
int arr1[10][10];
ifstream fin1;
fin1.open("array.txt", ios::ate);
if (!fin1) {
cerr << strerror(errno) << "\n"; // handle open errors
}
if (fin1.tellg() == 0) {
cout << "NULL" << endl;
ofstream fout1;
fout1.open("array.txt");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
fout1 << arr[i][j]; //if file is empty write
//matrix with zero value
arr1[i][j] = arr[i][j];
}
}
fout1.close();
}
else {
fin1 >> m1 >> n1;
for (int i = 0; i < m1; i++) // this is the code of reading of
for (int j = 0; j < n1; j++) //matrix from the file
{
fin1 >> arr[i][j];
arr1[i][j] = arr[i][j];
}
}
fin1.close();
inc(arr1); // updating matrix
ofstream fout2;
fout2.open("array.txt", ios::trunc | ios::out);
for (int i = 0; i < m1; i++)
for (int j = 0; j < n1; j++) { // write back to its file with
//updated value
arr[i][j] = arr1[i][j];
fout2 << arr[i][j];
}
fout2.close();
}
};
int main()
{
block b1;
int arr[10][10];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
arr[i][j] = 0;
b1.details(arr);
}
Here is an example of how to read a matrix from a file
#include <fstream>
#include <iostream>
#include <cstring>
int main() {
int arr[10][10];
for (std::size_t i = 0; i < 10; i++)
for (std::size_t j = 0; j < 10; j++)
arr[i][j] = 0;
std::size_t m1 = 10;
std::size_t n1 = 10;
std::ifstream fin1("array.txt");
if (!fin1) {
std::cerr << std::strerror(errno) << "\n"; // handle open errors
}
fin1 >> m1 >> n1;
for (std::size_t i = 0; i < m1; i++) // this is the code of reading of
for (std::size_t j = 0; j < n1; j++) { //matrix from the file
fin1 >> arr[i][j];
}
fin1.close();
for (std::size_t i = 0; i < 10; i++) {
for (std::size_t j = 0; j < 10; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << '\n';
}
}
with input file
10
10
1 2 3 4 5 6 7 8 9 0
11 2 3 4 5 6 7 8 9 0
21 2 3 4 5 6 7 8 9 0
31 2 3 4 5 6 7 8 9 0
41 2 3 4 5 6 7 8 9 0
51 2 3 4 5 6 7 8 9 0
61 2 3 4 5 6 7 8 9 0
71 2 3 4 5 6 7 8 9 0
81 2 3 4 5 6 7 8 9 0
91 2 3 4 5 6 7 8 9 0
I was trying to do the following:
Have a matrix, print the entire thing out, print at end of every row the biggest element of said row and print at the bottom of every column the smallest element of said column.
I'm pretty much a beginner at C++.
So here's what I've done so far:
#include <iostream>
#include <iomanip>
#define M 50
#define N 50
using namespace std;
int main()
{
int m,n;
int a[M][N];
int b[M],c[N];
do {
cout<<"m=";
cin>>m;
cout<<endl<<"n=";
cin>>n;
cout<<endl;
}
while(m!=n);
for(int i=0;i<m; i++) {
for(int j=0; j<n; j++){
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
}
int max_row;
max_row=0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] > max_row) {
max_row = a[i][j];
b[i] = max_row;
}
}
}
for (int i=0; i<m; i++)
{ for(int j=0; j<n; j++){
cout<<setw(3)<<a[i][j]<<"\t";
}
cout<<"|"<<b[i];
cout<<endl;
}
for(int i=0; i<m; i++){
cout<<setw(3)<<"-";}
cout<<endl;
for(int j=0; j<n; j++)
{cout<<c[j]<<"\t";
}
system("pause");
}
Most of the time the max_row are the correct ones such as this case:
3 2 1 |3
4 6 5 |6
7 8 9 |9
Other times they get messed up and it goes like this:
1 2 3 |3
4 33 6 |33
7 8 9 |-858993460
I really have no idea what causes it and since there are no error messages I got really confused. Also I have no idea how to make the min column ones. Any help would be appreciated.
The problem with these loops
max_row=0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] > max_row) {
max_row = a[i][j];
b[i] = max_row;
}
}
}
is that the value of max_row should be initialized with each iteration of the outer loop. Otherwise all rows after the first row deal with the maximum value of the previous row and in general can not have en element that is greater than the current value of max_row. So the corresponding element of the array b will not be initialized.
Also the user can enter for the matrix negative values in this case your program will output zeroes instead of maximum values.
To find maximum elements in rows and minimum elements in columns it is enough to have one pair of nested loops/
Here is a demonstrative program/
#include <iostream>
#include <iomanip>
int main()
{
const size_t N = 3;
int a[N][N] =
{
{ 1, 2, 3 },
{ 4, 33, 6 },
{ 7, 8, 9 }
};
int b[N], c[N];
for ( size_t i = 0; i < N; i++ )
{
b[i] = a[i][0];
c[i] = a[0][i];
for ( size_t j = 1; j < N; j++ )
{
if ( b[i] < a[i][j] ) b[i] = a[i][j];
if ( a[j][i] < c[i] ) c[i] = a[j][i];
}
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
std::cout << std::setw( 3 ) << a[i][j] << '\t';
}
std::cout << '|' << b[i] << '\n';
}
for ( size_t i = 0; i < N; i++ )
{
std::cout << std::setw( 3 ) << '-' << '\t';
}
std::cout << '\n';
for ( size_t i = 0; i < N; i++ )
{
std::cout << std::setw( 3 ) << c[i] << '\t';
}
std::cout << '\n';
return 0;
}
Its output is
1 2 3 |3
4 33 6 |33
7 8 9 |9
- - -
1 2 3
I need help in a C++ for a school task.
I don't really know where the error is.
It seems like it skips the first row.
I should compare the highest value with an other row's avarage value.
Task:
In the first row of the standard input there are the count of the cities (1≤N≤1000) and the count of days (1≤M≤1000). In the following N row there are the daily forecast M temperature values (-50≤Hi,j≤50).
In the standard output's first row, you have to write the city number, which maximal forecast has to be lower than some other city's avarage temperature! If there is none you should write -1!
Example:
Input
3 5
11 11 11 11 20
18 16 12 16 20
10 15 12 10 10
The code:
#include <iostream>
using namespace std;
int main() {
int N, M;
cin >> N;
cin >> M;
int homerseklet[N][M];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
cin >> homerseklet[i][j];
}
}
int maxh[N] = {0}, osszh[N] = {0};
for (int i = 0; i < N; i++)
{
maxh[i] = homerseklet[i][0];
for (int j = 0; j < M; j++)
{
osszh[i] = osszh[i] + homerseklet[i][j];
if (homerseklet[i][j] > maxh[i])
{
maxh[i] = homerseklet[i][j];
}
}
}
int atlag[N] = {0};
for (int i = 0; i < N; i++)
{
atlag[i] = osszh[i] / M;
}
bool van = false;
for (int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if (i != j && maxh[i] < atlag[j])
{
if (van = true)
{
cout << i + 1 << endl;
}
}
}
}
if (!van)
{
cout << -1 << endl;
}
return 0;
}
I have to compare matrix columns.
I tried many variations, but as far as I got, is when I compare "next to each other" columns.
// N rows
// M columns
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
int N, M, ok = 1;
const int maxn = 1000;
const int maxm = 1000;
short H[maxn][maxm];
cin >> N >> M;
for (int i=0; i<N; i++){
for (int j=0; j<M; j++){
cin >> H[i][j];
}
}
for (int j = 1; j < M; ++j)
{
ok = 1;
for (int i = 0; i < N; ++i)
{
if (H[i][j-1] >= H[i][j])
{
ok = 0;
}
}
if (ok)
{
cout << j+1 << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
I have to give back the index of the first column where is true, that the column's every element is greater than ANY OTHER column's elements. (Not summarized.)
For example:
10 10 12 15 10
11 11 11 13 20
12 16 16 16 20
It returns with 4, because the 4th column's every element is greater, than the 1st column's elements.
If there are none, it has to return with -1.
You need another inner loop to go through every other column:
#include <ios>
#include <iostream>
int main()
{
using namespace std;
ios_base::sync_with_stdio(false);
int N, M, ok = 1;
const int maxn = 1000;
const int maxm = 1000;
short H[maxn][maxm];
cin >> N >> M;
for (int i=0; i<N; i++){
for (int j=0; j<M; j++){
cin >> H[i][j];
}
}
for (int j = 0; j < M; ++j)
{
for (int j2 = 0; j2 < M; ++j2)
{
if (j == j2) continue;
ok = 1;
for (int i = 0; i < N; ++i)
{
if (H[i][j] <= H[i][j2])
{
ok = 0;
break;
}
}
if (ok)
{
cout << j + 1 << endl;
return 0;
}
}
}
cout << -1 << endl;
return 0;
}
Given the input:
3 5
10 10 10 15 10
11 11 11 13 20
12 16 16 14 20
This prints:
4
See it on Rextester
I am working on the problem at http://orac.amt.edu.au/cgi-bin/train/problem.pl?set=simple3&problemid=416. Below is the code which I came up. Sample data executed successfully but when I submit the code I am able to execute only 71% of the test cases. Not sure where went wrong. Please guide me.
Briefly the problem is as below:
I am trying to get the max friend list from the friend linking.
1 2
2 3
3 7
1 8
1 9
Output is 1 as it has more friend list
If the friend list match with max the list all.
1 2
2 3
3 7
1 8
9 10
Output should be. 1 2 3 as they have equal number of friends.
#include <bits/stdc++.h>
using namespace std;
int main(){
freopen("listin.txt","r",stdin);
freopen("listout.txt","w",stdout);
int f=0, a= 0, b= 0, m = 1;
long size = 1001;
cin >> f;
int arr[size] = {};
int x[size] = {};
for(int i = 0; i < f; i++){
cin >> a >> b;
arr[a] += 1;
arr[b] += 1;
}
for(int j = 0; j < size; j++){
if(m < arr[j]){
m = arr[j];
}
}
for(int j = 0; j < size; j++){
if(m == arr[j]){
x[j] = j;
}
}
for(int k = 0; k < size; k++){
if(x[k] > 0)
cout << x[k] << endl;
}
return 0;
}