I am having trouble with making a turn inside a two dimensional array to output the elements in spiral. I tried this code, but it is outputting not enough elements, I tried to make some if statements outside of the loop to cover all cases for which the general algorithm doesn't output. Can you help suggesting some way to manage the correct output.
CODE
#include <iostream>
#include <algorithm>
//#include <cmath>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c) / 2; runs--;) {
for (int i = c_beg; i < c_end; i++)
cout << m[r_beg][i] << " ";
for (int i = r_beg; i < r_end; i++)
cout << m[i][c_end] << " ";
for (int i = c_end; i > c_beg; i--)
cout << m[r_end][i] << " ";
for (int i = r_end; i > r_beg; i--)
cout << m[i][c_beg] << " ";
r_beg++;
c_beg++;
r_end--;
c_end--;
}
if (r <= c && c_beg <= c_end) {
for (int i = c_beg; i <= c_end; i++)
cout << m[r_end][i] << " ";
}
else if (r >= c && r_beg <= r_end) {
for (int i = r_beg; i <= r_end; i++)
cout << m[i][c_end] << " ";
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Example:
Input:
3 3
1 2 3
4 5 6
7 8 9
Output: 1 2 3 6 9 8 7 4 5
If you have for example 3x10 matrix. It doesn't output.
Input:
3
7
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
Output: should get to 13, but it stops at 8.
The code, as presented, loops towards center until the smaller of the two dimensions is consumed up. However, if that smaller dimension has odd size, then parts of the central row or column respectively haven't been printed out. You can cover that one with some special case handling after your outer loop:
for (int runs = std::min(r, c) / 2; runs--;)
{
// ...
}
if(c < r)
{
if(c & 1)
{
for (int i = r_beg; i <= r_end; i++)
// ^ (!)
// don't forget to print last element: there's no second loop
// that would print the corner element a second time now!
std::cout << m[i][c_end] << " ";
}
}
else
{
// handles the square matrix case as well
if(r & 1)
{
for (int i = c_beg; i <= c_end; i++)
std::cout << m[r_beg][i] << " ";
}
}
This can be solved by carefully fine-tuning the bail-out conditions / ranges of the for loops:
#include <iostream>
using namespace std; // :-(
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c);;)
{
for (int i = c_beg; i <= c_end; i++)
cout << " " << m[r_beg][i];
++r_beg;
for (int i = r_beg; i <= r_end; i++)
cout << " " << m[i][c_end];
--c_end;
if (!--runs) break;
for (int i = c_end; i >= c_beg; i--)
cout << " " << m[r_end][i];
--r_end;
for (int i = r_end; i >= r_beg; i--)
cout << " " << m[i][c_beg];
++c_beg;
if (!--runs) break;
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Input:
3 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Output:
1 2 3 4 5 6 7 14 21 20 19 18 17 16 15 8 9 10 11 12 13
Live Demo on ideone
Input:
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Notes:
I changed the bail-out of the for loop:
Instead of min(r, c) / 2, I use min(r, c) and decrement/check runs twice in the body.
I adjusted the update of r_beg, r_end, c_beg, and c_end.
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
Desired output:
2
4 2
6 4 2
8 6 4 2
Here is my code:
for(int i = 0; i <= 7; i += 2) {
for (int j = 0; j <= i; j += 2)
cout << j + 2 << " ";
cout << endl;
}
and I get a wrong answer.
Your approach with the double for loop was a good idea. Here is my generalized attempt, which allow to print what you want in n lines, rather than just 4:
#include <iostream>
using namespace std;
int main() {
int n = 4;
for(int i = 0; i < n; ++i) {
for(int j = i; j >= 0; --j) {
cout << 2 * (j + 1) << " ";
}
cout << "\n";
}
return 0;
}
Output:
2
4 2
6 4 2
8 6 4 2
Live Demo
PS: This approach minimizes (if not exterminates) the usage of magic numbers.
Welcome to Stack overflow.
You just went the wong way around with your inner for loop
#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i <= 7; i += 2){
for (int j = i; j >= 0; j -= 2) {
cout << j + 2 << " ";
}
cout << endl;
}
return 0;
}
this gives you the desired output:
2
4 2
6 4 2
8 6 4 2
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I need to print this table
2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
9 9 9 9
I have written this code for the following result
#include <iostream>
using namespace std;
int main(){
const int N = 9;
for(int i = 0; i <= N; i += 3){
for (int j = 0; j <= i; j +=3) {
cout << i << " ";
}
cout << endl;
}
cout << "\n";
for(int i = 2; i <= N; i += 2){
for (int j = i; j > 0; j -= 2) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
My Result:
0
3 3
6 6 6
9 9 9 9
2
4 2
6 4 2
8 6 4 2
Required Result:
2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
9 9 9 9
#include <iostream>
using namespace std;
int main(){
const auto n = 4;
auto count = 0;
for (auto i = 2; i <= n * 2; i += 2)
{
for (auto j = i; j > 0; j -= 2)
std::cout << j << " ";
std::cout << std::endl;
for (auto j = 0; j < (i == 2 ? i : i + 2); j += 3)
std::cout << count * 3 << " ";
++count;
std::cout << std::endl;
}
return 0;
}
Edit: Corrected...
A little near to the answer
2
0 ==> true
4 2
0 3 ==> should be 3 3
6 4 2
0 3 6 ==> should be 6 6 6
8 6 4 2
0 3 6 ==> should be 9 9 9
Here is another approach for case when you're providing number of repeats (like in #Ruks answer):
void printSequence(unsigned int repeats)
{
int n = 2;
for(int i = 1; i < repeats; i++)
{
n+=2*i;
}
//n - number of all numbers in sequence for given number of repeats
int step = 0;
int numsPerRow = 1;
for(int i = 0; i < n; i+=step)
{
for(int j = numsPerRow; j > 0; j--)
{
std::cout << 2*j << " ";
}
std::cout << std::endl;
for(int j = 0; j < numsPerRow; j++)
{
std::cout << step+numsPerRow-1 << " ";
}
std::cout << std::endl;
step+=2;
numsPerRow++;
}
}
Use this:
void print_sequence(unsigned long long const repeat = 4, unsigned long long const i = 0)
{
for (auto j = (i + 1ull) * 2ull; j > 2ull; j -= 2ull)
std::cout << j << " ";
std::cout << (repeat > 0ull && repeat < -1ull ? "2\n" : "");
for (auto j = 0ull; j < i; j++)
std::cout << i * 3ull << " ";
std::cout << (repeat > 0ull && repeat < -1ull ? std::to_string(i * 3ull) + "\n" : "");
if (repeat < -1ull && i + 1ull < repeat)
print_sequence(repeat, i + 1ull);
}
Edit: The shortest and strongest method I can think of...
And call it like this:
print_sequence();
If you don't want 4 times:
print_sequence(10)
It will repeat it as many times you want...
Kind regards,
Ruks.
I am writing a program for an assignment, which is:
Write a program that creates an int array of 20 elements, would ask the user how many tosses are required(1-20), fills the array with the result of tossing a number cube as many times as requested by the user, prints the values of the tosses horizontally then displays the longest run found
in the sequence of tosses.
For the above example the output would be if the user requested 18 tosses:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 5 5 4 3 1 2 2 2 2 6 1 3 3 5 5 5 5
The longest run occurs at index 14.
I wrote the program for the first part, but I am stuck on trying to find the index of the longest run. This is my code so far:
const int SIZE = 20;
int main()
int arr[SIZE];
int numToss;
srand(time(0));
cout << "How many tosses are required? Enter a value from 1 to 20: " << endl;
cin >> numToss;
while (numToss > SIZE || numToss < 1)
{
cout << "Please enter a value for the number of tosses greater than 1 and less than 20: " << endl;
cin >> numToss;
}
for (int i = 0; i < numToss; i++)
{
arr[i] = 1 + rand() % 6;
cout << " " << arr[i];
}
cout << endl;
int max_count = 0;
int length = arr[0];
for (int i = 0; i < length; i++)
{
int count = 1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > max_count)
max_count = count;
}
int result = 0;
for (int i = 0; i < length; i++)
{
int count=1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count == max_count)
{
max_count = arr[i];
result = i;
cout << "The longest run occurs at " << result << endl;
}
}
return 0;
It does not print correctly, how ca i fix this? Any help is appreciated!
How do i achieve this format when i keyed in int of ' 245 ':
(where if it is odd number it will be a rectangle, and even number will be triangle)
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
This is my code so far:
(I can't seemed to output triangle and rectangle at the same time)
int n;
int lastDigit;
do
{
cout << "Enter a positive integer: ";
cin >> n;
}while ( n <= 1 || n == '0');
cout << endl;
// If even digit - tri
do
{
lastDigit = n%10;
if (lastDigit / 2 ==0)
{
for (int i = 1; i <= lastDigit; ++i)
for (int tri = 1; tri <= i; ++tri)
cout << "\t" << tri;
cout << endl;
}
// if odd digit - rect
else if (lastDigit / 2 != 0)
{
for (int i = 1; i <= lastDigit; i++)
{
for (int rect = 1; rect <= i; rect++)
cout << "\t" << rect;
cout << endl;
}
n = n/10;
}
cout << endl;
}while (lastDigit != 0);
n = n/10;
cout << endl;
return 0;
And, how should i code when keyed in the int, the compiler will extract the first digit (From left to right) and output it accordingly?
Any help would be appreciated!
Following is the complete code.
Step1: Take user input and check whether it's odd or even.
Step2: If it is odd then perform triangle else rectangle.
#include<iostream>
int main () {
int n;
cout<<"Enter number: ";
cin>>n;
if (n % 2 != 0)
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= n; j++)
{
cout<<j<<" ";
}
}
}
else
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= i; j++)
{
cout<<j<<" ";
}
}
}
return 0;
}
Both of your print is similar. I suggest:
#include<iostream>
using namespace std;
int main () {
int n;
cout << "Enter number: ";
cin >> n;
int i = 1;
int& rowLim = ((n % 2) ? n : i);
for(i = 1; i <= n; i++)
{
cout << endl;
for(int j = 1; j <= rowLim; j++)
{
cout<<j<<" ";
}
}
return 0;
}
Simplest would be to use a string, iterate char by char and output accordingly. For instance
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
for (char c : s) {
int n = c - '0';
bool k = n % 2;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= (k ? n : i); ++j)
cout << " " << j;
cout << endl;
}
cout << endl;
}
return 0;
}
Output
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
See a DEMO