I am currently using C++ on a program called CodeZinger for one of my classes. I was asked to make a program that will output an array with input that the program gives me.
See screenshot below.
The issue is that the program outputs an extra space at the end of my array, which is making the program say that I have not gotten the question right.
#include <iostream>
using namespace std;
int main()
{
int rows = 1;
int cols = 1;
long int arr[100][100];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
My output (see screenshot above) is showing that my code is right and it was going well, except for that extra space at the end. Is there any way to remove that extra space without adding an if statement into my code somewhere?
Have the inner loop iterate until j < cols - 1 and then write one more output line after it ends, without a space (e.g.: std::cout << arr[i][cols-1];) –
UnholySheep
Related
Hi I'm trying to get my C++ console to output the first image I've attached. I know you have to like count the spaces in like maybe a for loop and decrement it as you go down the rows..However, I'm not exactly sure on how to go about that, pretty new to c++. The solution I'm currently working on is the exact mirror image of it. Any help would be greatly appreciated.
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
cout << "" << endl;
for (int j = 1; j <= i; ++j) {
cout << "*";
}
}
}
Correct Solution:
My Current Output:
You need an extra for loop before the one that is sending the asterisks, like so that will send the spaces to push the asterisks to the right.
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
for (int j = rows - i; j > 0; j--) {
cout << " ";
}
for (int j = 1; j <= i; ++j) {
cout << "*";
}
cout << endl;
}
}
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
cout << "" << endl; // Mistake here, you are putting a empty string
for (int j = 1; j <= i; ++j) {
cout << "*";
}
}
}
You're almost good. You have missed to put the spaces in order to create the right padding of the stars.
Here is the coding below :) I have also commented on some parts so that it is easier to understand the output of the code.
I have a slight idea that I need to use an "if statement" with "rand()%" in order to make sure that the program knows we want to calculate the sum of 1s and -1s only. for e.g using "rand()%2-1" can help with getting the total sum of 1s outputted in the table. Again, I'm not sure if this idea will work or not.
So the program should output something like "The amount of 1s in the table is 5 and the amount of -1s in the table is 3" for the first time its ran. Then when it is ran the second time, it could output something like "The amount of 1s in the table is 2 and the amount is -1s in the table is 5"
Sorry for any confusions and All your help will be highly appreciated :) :)
#include<iostream>
#include<iomanip>
#include<ctime>
using namespace std;
int main() {
srand(time(0));
const int ROWS=3;
const int COLS=4;
int table[ROWS][COLS];
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
table[i][j] = rand()%3-1;
}
}
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++)
cout << setw(3) << table[i][j];
cout << endl;
}
bool checkTable [ROWS][COLS];
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
if (table[i][j] !=0) {
checkTable[i][j] = true;
}
else{
checkTable[i][j] = false;
}
//The output in the line below is the final line outputted by the
console. This prints out "1" if there is a value in the index within
the table provided above (the value is represented by 1 or -1), and
prints out "0" if there is no value in the index (represented by 0)
cout << " " << checkTable[i][j];
}
}
return 0;
}
[...] for e.g using "rand()%2-1" can help with getting the total sum of 1s
outputted in the table.
I dont really understand what you mean by that. Counting and randomness dont go well together. I mean of course you can fill a matrix with random numbers and then do some counting, but rand() wont help anything for the counting.
You need something as simple as that:
int main() {
srand(time(0));
const int ROWS=3;
const int COLS=4;
int table[ROWS][COLS];
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) {
table[i][j] = rand()%3-1;
}
}
unsigned ones_counter = 0;
for (int i = 0; i < ROWS; i ++) {
for (int j = 0; j < COLS; j++) { // dont forget the bracket
cout << setw(3) << table[i][j];
if (table[i][j] == 1) { ones_counter++;} // <- this is counting
}
cout << endl;
}
std::cout << "number of 1s in the table : " << ones_counter << "\n";
....
I'm having trouble with one of my c++ assignments.
It's about decrypting a string of letters.
Here is a picture my teacher sketched up:
https://gyazo.com/33d90496958ef231dec7866e39ce1951
I must insert a string of letters using the command line. See the letters to the left on the picture i linked. They will be inserted in an array and it must this message: "DETTA ÄR KYPTERAT". It's in swedish, and it translates to "THIS IS ENCRYPTED".
The thing I'm having most trouble with is inserting the text into the multidimensional array using CIN.
It HAS to be a CIN in the beginning. Please answer in a simple and understandable as i'm still pretty novice at C++!
Without going into the details of your encryption algorithm, filling the 2d-array from standard input can be as follows:
int arr[ROWS][COLS] = {0};
char c;
for(int j = 0; j < COLS; j++)
{
for(int i = 0; i < ROWS; i++)
{
cin.get(c);
arr[i][j] = c;
}
}
// just output for testing
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
I suppose int type will be good for your algorithm, but of course it is just example, and you can do any changes.
I need some help. I need to make a modular program. But I have a little trouble. So, I need to read a 2D array but user defines row and column.
After the reading I need to make some calculations in another functions... but i can't write well a function which works...
I have tried with pointers... but I can't use well. I'm beginner.
//main
int a[2500]; //symbolic. n<=50 -in my case
int n;
reading (a*,n);
//reading function
void reading(int* array[], int &n)
{
cout << "n=<<;
cin >> n;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n ;j++) {
cin >> array[i][j];
}
}
}
Please help me.
There are a number of problems with this code. The most glaring one is that C++ != Python, so you have to put the main code inside a function like this:
int main() {
//Main code here, calling other functions etc
reading (&a,n);
return 0; //or return 1 to signal there has been an error
}
From there, you can work on your code. The good thing about a modular design is that you can debug parts of it and be sure each part works. Use a debugger, and ask a question with a single question in on SO if you are still stuck after doing some research too.
i got a right solution ;)
//main
int **matrix, n;
matrixread(matrix,n);
//matrixread
void matrixread(int** &matrix, int &m)
{
cin >> m;
matrix = new int*[m];
for (i = 0; i < m; i++)
{
matrix[i] = new int[m];
}
for (i = 0; i < m; i++)
{
for (j = 0; j < m; j++)
{
cout << "matrix[" << i << "," << j << "]= " ;
cin >> matrix[i][j];
}
}
I have file that contains 20x20 matrix with random numbers. I've read the numbers on the file and then to store in the array. It seems I had not actually assign the numbers to array, because when I had print one of the number it displayed something like "||" instead of number, see the line cout <<array[0][1]. My complete code is below:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
#define length 20
int main(){
char array[length][length];
char chs;
ifstream grid;
grid.open("D:\\Example\\digit.txt", ios::in);
while (!grid.eof()){
for (int i = 0; i < 19; i++){
for (int j = 0; j < 19; j++){
grid.get(chs);
if (!grid.eof()){
array[i][j] = chs;
cout << setw(1) << array[i][j];///It display the numbers on file, there is no problem here.*/
}
}
}
}
cout <<array[0][2];//There is problem it can not display the number it display something different than numbers.
grid.close();
while (1);
}
Output of the consol, the numbers on the file exactly look like this. cout <<array[0][3] does not print
I've changed the last part
cout << endl;
for (int i = 0; i < 19; i++){
for (int j = 0; j < 19; j++){
cout << array[i][j];
}
}
cout << endl;
grid.close();
Output of the last part that is different than numbers on the file
Your inner loop only runs for the current value of i, meaning that you read no values into the first row, 1 into the second row etc.
for (int i = 0; i < 19; i++){
for (int j = 0; j < i; j++){
// ^ wrong
If you want to read a 20x20 matrix, both your inner and outer loop should run for 20 iterations.
for (int i = 0; i < 19; i++){
for (int j = 0; j < 19; j++){
// ^^
Note also that you probably need to add some code to handle any newlines in your input file. After each set of 20 digits, you'll have one (\n) or two (\r\n) characters indicating a newline. These are a valid part of the text file but presumably aren't required to be stored in your array.