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.
Related
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
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 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.
I am learning C++ by myself,by solving different problems.
I am trying to solve problem which was originally designed for Pascal in C++.
It should ask user to input 3 integers M,N and q.
Then it should make 2d array of integers with size MxN, where all the elements of (I=I,...M) line will be the members of geometrical progression with first element equal to number of line (I) and denominator q.
I wanted to create a dynamic massive, but I realized that it won't really work with two undefined integers. So, I tried vectors. I guess that I created them in a right way, but I've got no idea how to make a geometrical progression.
Here is my code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int m, n, q;
cout << "Enter the number for M \n";
cin >> m;
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "This is not a number! " << endl;
system("pause");
return 0;
}
cout << "Enter the number for N \n";
cin >> n;
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "This is not a number! " << endl;
system("pause");
return 0;
}
cout << "Enter the number for Q \n";
cin >> q;
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "This is not a number! " << endl;
system("pause");
return 0;
}
int** matrix;
matrix = new int*[m];
for (int i = 0; i < m; i++)
matrix[i] = new int[n];
for (int i = 0; i < m; i++)
{
matrix[i][0] = i + 1;
}
for (int i = 0; i < m; i++)
{
for (int j = 1; j < n; j++)
{
matrix[i][j] = (i + 1)*pow(i, j);
cout << matrix[i][j];
}
}
system("pause");
return 0;
}
Note: You can create a two dimensional array of a variable size, although it involves memory allocation and is slightly ugly.
int** matrix;
matrix = new int*[M];
for (int i = 0; i < M; i++)
matrix[i] = new int[N];
That's the code to create an array of size MxN.
Don't forget to deallocate your memory like so:
for (int i = 0; i < M; i++)
delete matrix[i];
delete matrix;
As far as your question about the geometric progression, I am unsure of what you are asking. When you say geometric progression do you refer to something along the lines of 2 10 50 250 etc.? I am not sure what you mean by "lines" as you don't refer to any such variable in your code.
EDIT
So once the MxN matrix is created, iterate through the rows and initialize the rows like so:
for (int i = 0; i < M; i++)
{
matrix[i][0] = i+1;
}
This should set the first column of each row to the correct number.
Then something along the lines of this should fill out the rest of the geometric progression:
for (int i = 0; i < M; i++)
{
for (int j = 1; j < N; j++)
{
matrix[i][j] = (i+1)*pow(r,j);
//note that you'll probably have to do some typecasting
//p.s. I'm not 100% sure that this is the correct formula
}
}
I think this is what you are looking for. Let me know if it works because I haven't tested it myself.
Print the matrix like this:
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
std::cout << matrix[i][j] << " ";
}
std::cout << "\n";
}