what would be the logic for the given output - c++

here is an output of a pattern program. I'm finding it hard to find the logic for the below ouput. please write the code in C++ ...
output:
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15

#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n+1][n+1];
int st, ed, inc;
int num = 1;
for(int j = 1; j <= n; j++) {
if(j%2==1) {
st = j, ed = n+1, inc = 1;
} else {
st = n, ed = j-1, inc = -1;
}
for(int i = st; i != ed; i += inc) {
arr[i][j] = num;
num++;
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Input: 5
Output:
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15

Related

How to read the matrix from file and make some change in it and again write it to the file?

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

Friendlist in AIO challenge

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

Fill an array diagonally

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
*/

C++ Magic Square from txt: Only reads first square

This is related to another question I've asked, but now I've gotten a little farther. I am trying to create a Magic Square program based on a text file input. The program will read the first square but then prints out gibberish for the rest of them. Where there is an individual number, it's supposed to set the parameters for the square following it.
So here's an example from the text file:
3 <- this indicates the rows&columns
4 9 2 <- row 1
3 5 7 <- row 2
8 1 6 <- row 3
5 <- start of next square. rows&columns
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
Here's the program that I have so far.
I'm trying to follow the assignment instructions, which is why I have 2d arrays and functions set up the way I do
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int SIZE = 20;
void readSquare(int, int[][SIZE]);
void printSquare(int, int[][SIZE]);
bool checkMagic(int, int[][SIZE]);
int sumRow(int, int, int[][SIZE]);
int sumColumn(int, int, int[][SIZE]);
int sumDiagonal1(int, int[][SIZE]);
int sumDiagonal2(int, int[][SIZE]);
int main()
{
int n;
int square[SIZE][SIZE];
ifstream inputFile;
inputFile.open("Prog2Input.txt");
while (inputFile >> n)
{
readSquare(n, square);
printSquare(n, square);
if (checkMagic(n, square))
{
cout << "Magic Square" << endl;
}
else
{
cout << "NOT Magic Square" << endl;
}
system("pause");
}
system("pause");
return 0;
}
void readSquare(int n, int square[][SIZE]) {
ifstream inf("Prog2Input.txt");
int x;
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++) {
inf >> x;
square[i][j] = x;
}
}
}
void printSquare(int n, int square[][SIZE]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << square[i][j] << " ";
}
cout << endl;
}
}
bool checkMagic(int n, int square[][SIZE]) {
int total = ((1 + n*n) / 2)*n;
for (int i = 0; i < n; i++) {
if (sumRow(i, n, square) != total) {
return false;
}
}
for (int i = 0; i<n; i++) {
if (sumColumn(i, n, square) != total) {
return false;
}
}
if (sumDiagonal1(n, square) != total) {
return false;
}
if (sumDiagonal2(n, square) != total) {
return false;
}
return true;
}
int sumRow(int row, int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[row][i];
}
return sum;
}
int sumColumn(int col, int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][col];
}
return sum;
}
int sumDiagonal1(int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][i];
}
return sum;
}
int sumDiagonal2(int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][(n - i) - 1];
}
return sum;
}
All, I appreciate your help in trying to resolve this. I took your comments in combination with my professors to make the below code. This will process an unknown number of magic square matrices specified by an initial size integer.
Sample text file first:
3 <- this indicates the rows&columns
4 9 2 <- row 1
3 5 7 <- row 2
8 1 6 <- row 3
5 <- start of next square. rows&columns
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
Now the final product of the code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int SIZE = 20;
ifstream inf;
void readSquare(int, int[][SIZE]);
void printSquare(int, int[][SIZE]);
bool checkMagic(int, int[][SIZE]);
int sumRow(int, int, int[][SIZE]);
int sumColumn(int, int, int[][SIZE]);
int sumDiagonal1(int, int[][SIZE]);
int sumDiagonal2(int, int[][SIZE]);
int main()
{
int n;
int square[SIZE][SIZE];
inf.open("Prog2Input.txt");
while (inf >> n)
{
cout << "Matrix Size: " << n << endl;
readSquare(n, square);
printSquare(n, square);
if (checkMagic(n, square))
{
cout << "Magic Square" << endl;
}
else
{
cout << "NOT Magic Square" << endl;
}
cout << endl;
}
system("pause");
return 0;
}
void readSquare(int n, int square[][SIZE]) {
int x;
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++) {
inf >> x;
square[i][j] = x;
}
}
}
void printSquare(int n, int square[][SIZE]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << square[i][j] << " ";
}
cout << endl;
}
}
bool checkMagic(int n, int square[][SIZE]) {
int total = ((1 + n*n) / 2)*n;
for (int i = 0; i < n; i++) {
if (sumRow(i, n, square) != total) {
return false;
}
}
for (int i = 0; i<n; i++) {
if (sumColumn(i, n, square) != total) {
return false;
}
}
if (sumDiagonal1(n, square) != total) {
return false;
}
if (sumDiagonal2(n, square) != total) {
return false;
}
return true;
}
int sumRow(int row, int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[row][i];
}
return sum;
}
int sumColumn(int col, int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][col];
}
return sum;
}
int sumDiagonal1(int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][i];
}
return sum;
}
int sumDiagonal2(int n, int square[][SIZE]) {
int sum = 0;
for (int i = 0; i<n; i++) {
sum += square[i][(n - i) - 1];
}
return sum;
}

Thread 1: EXC_BAD_ACCESS (code=1, adress=0x0)

I'm making Dijikstra algorithm in c++ (XCode).
I'm getting exc_bad_access code=1 at that moment:
for (k=0; k<M; k++){
input>>i>>j>>V;
Graf[i][j] = V;
}
Whole code:
#include <iostream>
#include <fstream>
int min(int[]);
int **Graf;
int *Label;
int *Active;
int i, j, k;
int Start, N, M, V, Last;
using namespace std;
int min(int array[]) {
int min, k, min_pos = -1;
min = 32767;
for (k=0; k<M; k++) {
if (array[k] < min && Active[k] == 1){
min = array[k];
min_pos = k;
}
}
return min_pos;
}
int main(){
ifstream input ("input.txt");
input>>N>>M>>Start>>Last;
if (!input){
cout << "File not found" << endl;
return 1;
}
Graf = new int *[N];
for (i=0; i<N; i++){
Graf[i] = new int [N];
}
for (i=0; i<N; i++){
for (j=0; j<N; j++){
Graf[i][j]=0;
}
}
Label = new int [N];
Active = new int [N];
for (i=0; i<N; i++){
Label[i] = 0;
Active[i] = 0;
}
for (k=0; k<M; k++){
input>>i>>j>>V;
Graf[i][j] = V;
}
for (i=0; i<M; i++){
Label[i] = 32767;
Active[Start] = 1;
}
Label[Start] = 0;
i = Start;
do
{
for (j=0; j<N; j++)
if (Graf[i][j] != 0 && Label[j] > Label[i] + Graf[i][j]){
Active[j] = 1;
Label[j] = Label[i] + Graf[i][j];
}
Active[i] = 0;
i=min(Label);
}
while (i != -1);
cout << Label[Last] << endl;
return 0;
}
I'm looking for a mistake for some days, but can't find.
Input.txt
5 9 0 4
0 0 5 2 4 0
0 0 0 7 0 6
5 0 0 9 1 15
2 7 9 0 0 12
4 0 1 0 0 8
0 6 15 12 8 0
I believe your error is in your input file: you read i and j and use it to access your arrays, without verifying that these value are within expected boundaries.
For instance, if you improve your questioned code with an error message, like:
for (k=0; k<M; k++){
input>>i>>j>>V;
if (i>=N || j>=N) {
cerr << "Bad Input for k="<<k<<": "<<i<<","<<j<<","<<V<<endl;
continue;
}
//assert(i<N && j<N); variant if you don't care for the values
Graf[i][j] = V;
}
You'll get the following results:
Bad Input for k=3: 7,0,6
Bad Input for k=4: 5,0,0
Bad Input for k=5: 9,1,15
Bad Input for k=6: 2,7,9