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];
}
}
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
using namespace std;
int main() {
int t;
cin >> t;
for (int j = 0; j < t; j++) {
int n;
cin >> n;
vector<string> cities;
for (int i = 0; i < n; i++) {
cin >> cities[i];
}
int size = cities.size();
for (int i = 0; i < n; i++) {
for (int k = 1; k < n; k++) {
if (cities[i] == cities[k]) size--;
}
}
cout << size << '\n';
}
}
So, this task is from kattis website(link: https://open.kattis.com/problems/everywhere) and the goal is to get the number of the cities someone has visited, the problem is that some of these cities were visited twice or even more, so the only thing we need is to get the number of cities.
That's how input looks like:
7
saskatoon
toronto
winnipeg
toronto
vancouver
saskatoon
toronto
3
edmonton
edmonton
edmonton
And my code doesn't let me input the whole input, I mean, I input 3 values and then program stops working. Can you help me?
You forgot to initialize your vector with a size. You can do it by passing n to the constructor:
vector<string> cities(n);
My source code is below:
string** field = new string*[m]; //initialise a mxn Matrix=> field.
for (int i = 0; i < m; i++) { // we do this because the compiler does not know the ammount of memory required in advance hence use of poitners.
field[i] = new string[n];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> &field[i][j];
}
}
I am having trouble trying to push the input from cin to my 2d matrix field. I have tried double dereferencing ie. &&field, but still get the same error. Any assistance would be greatly appreciated.
I initially wrote cin >> field[i][j] but got confused because intellisense said no operator matches operands of type std::istream and std::string. To solve that problem I had to import string The corrected code is below:
#include <string>;
//...
string** field = new string*[m]; //initialise a mxn Matrix=> field.
for (int i = 0; i < m; i++) { // we do this because the compiler does not know the ammount of memory required in advance hence use of poitners.
field[i] = new string[n];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> field[i][j];
}
}
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 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";
}