I want to write a code that print a table like this:
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
I wrote a code to print a table as said above, but it just print 5's.
I know that I have to use a condition to print such a table. What's the condition to print it ?
int main () {
int number = 5;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (condition)
...
else
cout << number << " ";
}
cout << endl;
}
return 0;
}
As I mentioned in comments, what you want to print is the Chebyshev distance to the center +1. I dont know what condition can make your code work, but instead I would use a simple formula to calculate each value:
#include <iostream>
using namespace std;
int main() {
for (int i=0;i<9;i++){
for (int j=0;j<9;j++){
cout << std::max(abs(i-4),abs(j-4)) +1 << " " ;
}
cout << endl;
}
}
/*To make boxes and inside box and so on
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int a[100][100],n,i,j,k=0,l;
clrscr();
cout<<"Enter the outside No. n \n";
//like for your answer it is 5;
//i and j are loop element for 2D array
//k is used for making n boxes
cin>>n;
l=n;
n=2*n-1;
while(k<n)
{
if(k%2==0)
{
for(i=k;i<n-k;i++)
{
a[0+k][i]=l;
a[i][0+k]=l;
a[n-1-k][i]=l;
a[i][n-1-k]=l;
}
k++;l--;
}
else
{
for(i=k;i<n-k;i++)
{
a[0+k][i]=l;
a[i][0+k]=l;
a[n-1-k][i]=l;
a[i][n-1-k]=l;
}
k++;l--;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout << a[i][j];
if(a[i][j]>9)
cout<<" ";
else
cout<<" ";
}
cout<<endl;
}
getch();
}
Related
I am writing some C++ code and i currently have a function that reads in some numbers from a text file and stores them into a 2D array. I now need to output the same numbers i have stored into the 2D array back out into a new text file. I currently have some code in a function that can output the numbers however they are not in the same format as the input file. As you can see below.
Input File Format (space between each number)
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
Output File Format Currently
123456789234567891345678912456789123567891234678912345789123456891234567912345678 (this is all on one line)
My function to read in from the text file.
void Grid::LoadGrid(const char filename[])
{
ifstream file(filename);
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
{
file >> m_grid[x][y];
}
}
file.close();
}
My function to read out to the text file. (m_grid is the 2D array)
void Grid::SaveGrid(const char filename[])
{
ofstream file(filename);
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
{
file << m_grid[x][y];
}
}
file.close();
}
If anyone can help me output it to the text file so it will appear the same as the input i'd be very grateful.
Edit: Question has been answered.
After your inner loop completes
file << endl;
Also in your inner loop might want to..
file << m_grid[x][y] << " ";
Update the way you write to the file like this:
void Grid::SaveGrid(const char filename[])
{
ofstream file(filename);
for (int y = 0; y < 9; y++) {
cout << endl;
for (int x = 0; x < 9; x++)
{
cout << m_grid[x][y];
cout << " ";
}
}
file.close();
}
In your output function you should write the spaces after each character. And the newline character when you reach the end of a row.
I believe this would work
void Grid::SaveGrid(const char filename[])
{
ofstream file(filename);
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
file << m_grid[x][y] << " ";//write a space after the character
if(x ==8) //at x ==8 is where this for loop would exit
{
file << "\n"
}
}
file.close();
}
The output you are seeing is exactly what you told the compiler to do. You are displaying each of your NxN matrix elements one right after another. You never added any white space to your output stream. To fix this, it is quite simple. Adjust your function as follows:
void Grid::SaveGrid(const char filename[])
{
ofstream file(filename);
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
{
file << m_grid[x][y] << " "; // and a space after each element
}
file << '\n'; // add a new line character after each row has been printed.
}
file.close();
}
Here's a simple working example that uses a std::vector<int> of your numbers and displays it to the console. The only difference here is that I'm using a 2D to 1D mapping for the indexing into the vector. Once you understand how this affects the formatting it should be trivial to convert it from an std::vector to a multidimensional array and to convert the iostream to a fstream output.
#include <iostream>
#include <vector>
int main() {
const std::vector<int> values{ 1,2,3,4,5,6,7,8,9,
2,3,4,5,6,7,8,9,1,
3,4,5,6,7,8,9,1,2,
4,5,6,7,8,9,1,2,3,
5,6,7,8,9,1,2,3,4,
6,7,8,9,1,2,3,4,5,
7,8,9,1,2,3,4,5,6,
8,9,1,2,3,4,5,6,7,
9,1,2,3,4,5,6,7,8
};
const int size = 9;
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
std::cout << values[col + size*row] << " ";
}
std::cout << '\n';
}
return 0;
}
-Ouput-
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
I was solving subarray with given sum,Where we have to print the starting and ending index of array if subarray with sum is found , when I tried with two test cases simultaneously i got wrong result
But when I was tried one at a time I got right answer in both.
You please also check in your IDE this is happening in every IDE.
Testcase (Simultaneously)
2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
Output
2 4 (expected 2 4)
2 5 (But expected 1 5)
But when I tried like this for second test cases
1
10 15
1 2 3 4 5 6 7 8 9 10
Output : 1 5(As expected)
I got correct answer ,why my program this kind of weird behaviour ?
#include<iostream>
#include<vector>
#include<queue>
#include<unordered_map>
using namespace std;
vector<int>a;
unordered_map<int, int>seen;
int main()
{
int t;
cin >> t;
while (t--) {
int n, s;
cin >> n >> s;
a.resize(n);
int sum = 0;
seen[0] = -1;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if (seen.find(sum - s) != seen.end()) {
int x;
x = seen[sum - s] + 2;
cout << x << " " << i + 1 << endl;
break;
}
else {
seen[sum] = i;
}
}
seen.clear();
a.clear();
//cout<<endl;
}
return 0;
}
I am making a 6x6 dice game and I need to be able to put ROWS on the y-axis of the table but I do not understand how to do that with my code.
Create a 6x6 2D-Table that holds the sum of the rows and columns using values 1 to 6.
I have been reading through the ninth edition of "Starting out with C++ From Control Structures through Objects" by Tony Gaddis and I just cannot find anything about what I am looking for.
//System Libraries
#include <iostream> //Input/Output Library
#include <iomanip> //Format Library
using namespace std;
//User Libraries
//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
const int COLS=7;
//Function Prototypes
void fillTbl(int [][COLS],int);
void prntTbl(const int [][COLS],int);
//Execution Begins Here!
int main(int argc, char** argv) {
//Declare Variables
const int ROWS=6;
int tablSum[ROWS][COLS] ={{1,2,3,4,5,6,7},
{2,3,4,5,6,7,8},
{3,4,5,6,7,8,9},
{4,5,6,7,8,9,10},
{5,6,7,8,9,10,11},
{6,7,8,9,10,11,12}};
//Initialize or input i.e. set variable values
fillTbl(tablSum,ROWS);
cout<<"Think of this as the Sum of Dice Table\n";
cout<<" C o l u m n s\n";
cout<<" | 1 2 3 4 5 6\n";
cout<<"----------------------------------\n";
//Display the outputs
prntTbl(tablSum,ROWS);
//Exit stage right or left!
return 0;
}
void fillTbl(int tablSum [][COLS],int ROWS)
{
cout<<"";
}
void prntTbl(const int tablSum [][COLS],int ROWS)
{
for(int x = 0; x < ROWS; x++)
{
for(int y = 0; y < COLS; y++)
{
cout<<setw(4)<<tablSum[x][y];
}
cout<<endl;
}
}
Your Output
Think·of·this·as·the·Sum·of·Dice·Table↵
···········C·o·l·u·m·n·s↵
·····|···1···2···3···4···5···6↵
----------------------------------↵
···1···2···3···4···5···6···7↵
···2···3···4···5···6···7···8↵
···3···4···5···6···7···8···9↵
···4···5···6···7···8···9··10↵
···5···6···7···8···9··10··11↵
···6···7···8···9··10··11··12↵
Expected Output
Think·of·this·as·the·Sum·of·Dice·Table↵
···········C·o·l·u·m·n·s↵
·····|···1···2···3···4···5···6↵
----------------------------------↵
···1·|···2···3···4···5···6···7↵
R··2·|···3···4···5···6···7···8↵
O··3·|···4···5···6···7···8···9↵
W··4·|···5···6···7···8···9··10↵
S··5·|···6···7···8···9··10··11↵
···6·|···7···8···9··10··11··12↵
We can change your prntTbl function to have a string literal containing the rows string with: char* rows = " ROWS "; Then before every inner loop iteration, we can print the character at the index of the string using our first loop index, as well as the row value and any spacing using: cout << rows[x] << " " << x + 1 << " |";
Our ending prntTbl method looks like:
void prntTbl(const int tablSum [][COLS],int ROWS)
{
char* rows = " ROWS ";
for(int x = 0; x < ROWS; x++)
{
cout << rows[x] << " " << x + 1 << " |";
for(int y = 0; y < COLS; y++)
{
cout<<setw(4)<<tablSum[x][y];
}
cout<<endl;
}
and the output:
C o l u m n s
| 1 2 3 4 5 6
----------------------------------
1 | 1 2 3 4 5 6 7
R 2 | 2 3 4 5 6 7 8
O 3 | 3 4 5 6 7 8 9
W 4 | 4 5 6 7 8 9 10
S 5 | 5 6 7 8 9 10 11
6 | 6 7 8 9 10 11 12
Hi guys i've to calculate the longest sequence of numbers without any repetitions and return the size of the sub-segment.
The point is that im missing something at some point but I don't know where.
int resolverCaso() {
int num;
int cont = 0;
cin >> num;
int var;
TreeMap<int,int> a;
int aux;
int max = 0;
for (int i = 0; i < num; i++) {
cin >> var;
if (!a.contains(var)) {
a[var] = i;
aux = var;
cont++;
}
else {
if (a[aux]==i-1 && var==aux) {
cont = 1;
a = TreeMap<int, int>();
a[var] = i;
}
else {
a.erase(var);
a[var] = i;
}
}
if (cont > max) {
max = cont;
}
}
return max;
}
I've tried the following cases with this outputs and everything seems to be ok.
E:1 2 3 1 2 3 O:3
E:2 2 2 2 O:1
E:4 5 6 7 6 O:4
E:7 8 9 10 7 8 9 11 2 O:6
E:7 8 9 10 10 10 1 2 3 4 O:5
E:3 4 2 3 4 2 8 9 10 11 O:7
E:0 O:0 ( empty vector ).
E:1 O:1
So basically im looking for some sequence that doesn't work with my code.
Thanks.
The problem is with
else {
a.erase(var);
a[var] = i;
}
You need to do more here. Try the sequence 1 3 4 2 3 4 2 8 9 10 11.
I looked up in many places and tried to understand how to get arbitrary number of nested for loops via recursion. But what I have understood is clearly wrong.
I need to generate coordinates in an n-dimensional space, in a grid-pattern. The actual problem has different coordinates with different ranges, but to get simpler things right first, I have used the same, integer-stepped coordinate ranges in the code below.
#include <iostream>
using namespace std;
void recursion(int n);
int main(){
recursion(3);
return 0;
}
void recursion(int n)
{
if(n!=0){
for(int x=1; x<4; x++){
cout<<x<<" ";
recursion(n-1);
}
}
else cout<<endl;
}
I want, and was expecting the output to be:
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
Instead, the output I'm getting is
1 1 1
2
3
2 1
2
3
3 1
2
3
2 1 1
2
3
2 1
2
3
3 1
2
3
3 1 1
2
3
2 1
2
3
3 1
2
3
I just can't figure out whats wrong. Any help to figure out the mistake or even another way to generate coordinates will be greatly appreciated. Thanks!
Non-recursive solution based on add-with-carry:
#include <iostream>
using namespace std;
bool addOne(int* indices, int n, int ceiling) {
for (int i = 0; i < n; ++i) {
if (++indices[i] <= ceiling) {
return true;
}
indices[i] = 1;
}
return false;
}
void printIndices(int* indices, int n) {
for (int i = n-1; i >= 0; --i) {
cout << indices[i] << ' ';
}
cout << '\n';
}
int main() {
int indices[3];
for (int i=0; i < 3; ++i) {
indices[i] = 1;
}
do {
printIndices(indices, 3);
} while (addOne(indices, 3, 3));
return 0;
}
Recursive solution, salvaged from your original code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void recursion(int n, const string& prefix);
int main(){
recursion(3, "");
return 0;
}
void recursion(int n, const string& prefix)
{
if (n!=0) {
for(int x=1; x<4; x++){
ostringstream os;
os << prefix << x << ' ';
recursion(n-1, os.str());
}
}
else cout << prefix << endl;
}
Per Igor's comment, you need an increment function.
Let's use an std::vector to represent each dimension. That is vector[0] is the first dimension, vector[1] is the second dimension and so on.
Using a vector allows us to determine the number of dimensions without any hard coded numbers. The vector.size() will be the number of dimensions.
Here is a function to get you started:
void Increment_Coordinate(std::vector<int>& coordinates,
int max_digit_value,
int min_digit_value)
{
unsigned int digit_index = 0;
bool apply_carry = false;
do
{
apply_carry = false;
coordinates[digit_index]++; // Increment the value in a dimension.
if (coordinates[digit_index] > max_digit_value)
{
// Reset the present dimension value
coordinates[digit_index] = min_digit_value;
// Apply carry to next column by moving to the next dimension.
++digit_index;
apply_carry = true;
}
} while (apply_carry);
return;
}
Edit 1
This is only a foundation. The function needs to be boundary checked.
This function does not support dimensions of varying sizes. That is left as an exercise for reader or OP.