I am trying to make a program where it asks the user the number of shelves and number of positions on those shelves from the user then it asks the user where he wants to enter a product.
Now I got rows and columns figured out but I can't get the full product name printed it just shows a single character of said product. Is there a way I can store an address of a string variable and store it in that column of my row to print the complete word?
This is what I have done so far.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int M = 4;
int N = 4;
char *item;
char ie[20];
// dynamically create an array of pointers of size `M`
char** A = new char*[M];
// dynamically allocate memory of size `N` for each row
for (int i = 0; i < M; i++) {
A[i] = new char[N];
}
cout << "Row num" << endl;
int row = 0;
cin >> row;
cout << "Row col" << endl;
int col = 0;
cin >> col;
//string temp;
cout << "Item" << endl;
cin >> ie;
for (int j = 0; j < strlen(ie); j++)
{
A[row][col] = ie[j];
}
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
}
sorry If the question is not clear or easy to understand.
First recommendation: never do like this
for (int j = 0; j < strlen(ie); j++) { /* do something */ }
Do it like this instead:
for (int i = 0; ie[i] != '\0'; ++i) { /* do something */ }
or
int ie_len = strlen(ie);
for (int j = 0; j < ie_len; ++j) { /* do something */ }
because each loop condition check it calls strlen which takes length of string to calculate this function, so it takes square of string length in first case
I recommend use std::string and std::vector to store 2D array of strings, and avoid raw pointers and especially new operator (at least for code in question body you forgot delete for this new's). If you really want mess with raw pointers, see the end of this answer, but now recommended way to do what you want (as I understand what you want):
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int M = 4;
int N = 4;
std::vector<std::vector<std::string>> A(M);
char ie[20];
for (int i = 0; i < M; i++) {
A[i] = std::vector<std::string>(N);
}
// can be used std::fill(A.begin(), A.end(), std::vector<std::string>(N)); instead for loop above but for loop simplier to understand
cout << "Row num" << endl;
int row = 0;
cin >> row;
cout << "Row col" << endl;
int col = 0;
cin >> col;
string temp;
cout << "Item" << endl;
cin >> temp;
A[row][col] = temp;
for (int i = 0; i < M; i++)
{
cout << std::string(N * (temp.size() + 3) + 1, '-') << "\n"; // add separator to be clear what happening
cout << "| "; // add separator to be clear what happening
for (int j = 0; j < N; j++) {
A[i][j].resize(temp.size(), ' '); // for all columns has equal width to be clear what happening
cout << A[i][j] << " | ";
}
cout << endl;
}
}
example of input and output of this code:
Row num
1
Row col
2
Item
bob
-------------------------
| | | | |
-------------------------
| | | bob | |
-------------------------
| | | | |
-------------------------
| | | | |
Messing about raw pointers (your code with some editions and useful comments):
#include<iostream>
#include<cstring> // cstring not string as #PaulMcKenzie mentioned in comments for strlen
using namespace std;
int main()
{
int M = 4;
int N = 4;
char *item;
char ie[20];
// dynamically create an array of pointers of size `M`
const char*** A = new const char**[M]; // need add one more star because two for two-dimensonal array and one more for ~string~(aka char array)
// dynamically allocate memory of size `N` for each row
for (int i = 0; i < M; i++) {
A[i] = new const char*[N];
}
const char empty_string[1] = "";
for (int i = 0; i < M; ++i){
for (int j = 0; j < N; ++j){
A[i][j] = empty_string; // make empty names on all shelves
}
}
cout << "Row num" << endl;
int row = 0;
cin >> row;
cout << "Row col" << endl;
int col = 0;
cin >> col;
//string temp;
cout << "Item" << endl;
cin >> ie;
/*not need this:
for (int j = 0; j < strlen(ie); j++)
{
A[row][col] = ie[j];
}
just save adress like this:*/
A[row][col] = ie;
int ie_len = strlen(ie);
/* can be done simply:
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++) {
cout << A[i][j] << " ";
}
cout << endl;
}
but it doesn't look like table*/
// add seprators for understanding what happenning
for (int i = 0; i < M; i++)
{
// add horizontal separator
for (int j = 0; j < N * (strlen(ie) + 3) + 1; ++j){
cout << "-";
}
cout << "\n";
cout << "| "; // add separator to be clear what happening
for (int j = 0; j < N; j++) {
cout << A[i][j];
// for equal width of columns add some spaces
for (int k = strlen(A[i][j]); k < ie_len; ++k){
cout << " ";
}
cout << " | ";
}
cout << endl;
}
// not forget to delete all what u newed
for (int i = 0; i < M; i++) {
delete A[i];
}
delete A;
}
I am again here with this query.
In the following code, I initialize my matrix with all values equals zero and after it, I write this matrix in a text file. Now I want to read that matrix from the file and put all the values in another matrix named arr1[][] and when I print it on the screen I got a blank screen why?
Please help me thanks in advance.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
int n = 10;
int m = 10;
int arr[10][10];
int arr1[10][10];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
arr[i][j] = 0; // here i initialized my matrix with all values zero.
}
ofstream fout;
fout.open("array.txt");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
fout << arr[i][j]; // here i write it into the file
}
cout << endl;
}
fout.close();
ifstream fin;
fin.open("array.txt");
if (!fin)
{
cerr << std::strerror(errno) << "\n"; // handle open errors
}
fin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
fin >> arr[i][j]; // now here i read the matrix and put it into the
// different matrix.
arr1[i][j] = arr[i][j];
}
fin.close();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << arr1[i][j]; // when i print it to the screen i got black screen
// but i wants
// a matrx which written in the file.
}
cout << endl;
}
return 0;
}
I am trying to make the function output numbers in a form of a matrix, not just a line
void SaveMatrix(TMatrix* mat){
ofstream SaveM;
SaveM.open("Matrix.txt", ios::out);
if (SaveM.is_open()){
for (int i=0; i<mat->line; ++i){
for (int j=0; j<mat->column; ++j){
SaveM<< mat->m[i][j]<<" ";
}
}
}else{
cout<<"file is open"<<endl;
}
}
I tried to put this in the second for-cycle, without a result
if(j==mat->column){
SaveM<<endl;
}
The matrix is declared:
struct TMatrix {
double* *m;
int line;
int column;
};
j will never reach mat->column in your inner for loop.
I think you could use something like this:
for (int i = 0; i < mat->line; i++){
SaveM << "[";
for (int j = 0; j < mat->column; j++){
SaveM << mat->m[i][j]);
if (j < sizeMatrix - 1){
SaveM << ", ";
}
}
SaveM << "]" << endl;
}
After trying a lot of times and thinking again and again.. It feels like a frustrated person.
I'm here to get some suggestions from you guys..
Actually I'm trying to find the maximum of each ROW and COLUMN.
So by using divide and conquer technique, I write two separate functions one for finding max of each row and store it to row vector that i used as an argument. Respectively for columns.
void maxValuesR(int a[][cols], int rv[], int row)
void maxValuesC(int a[][cols], int cv[], int row)
PROBLEM: The code is not even compile, I just don't understnd the errors..
Please Help..
I Really Need Your Help Here!
The code is as follow:
#include <iostream>
using namespace std;
const int rows = 2; // Declared As Global Variable
const int cols = 3; // Declared As Global Variable
void getData(int arr[][cols], int rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
cout << "Enter Element: ";
cin >> arr[i][j];
}
}
}
void printData(int a[][cols], int rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
}
void print_single(int a[], int row)
{
for(int i = 0; i < row; i++)
{
cout << a[i] << "\t";
}
}
void maxValuesR(int a[][cols], int rv[], int row)
{ int foxi;
for(int i = 0; i < row; i++)
{
foxi = a[i][0];
for(int j = 0; j < cols; j++)
{
if(foxi < a[i][j])
{
foxi = a[i][j];
}
}
rv[i] = foxi;
}
}
void maxValuesC(int a[][cols], int cv[], int row)
{
int maxi;
for(int i = 0; i < cols; i++)
{
maxi = a[0][i];
for(int j = 0; j < row; j++)
{
if(maxi > a[j][i])
{
maxi = a[j][[i];// show error here => expected a '{' introducing lambda body
}
}
cv[i] = maxi;
}
}
int main()
{
int rowVector[rows];
int colVector[cols];
int a[rows][cols];
cout << "Fill Array_1. " << endl;
getData(a, rows);
cout << "Array_1." << "\n\n";
printData(a, rows); cout << endl;
maxValuesR(a, rowVector, rows);
print_single(rowVector, rows);
cout << "\n\n";
maxValuesC(a, colVector, rows);
print_single(colVector, rows);
return 0;
}
To clarify the compile errors:
You forgot the include (or didn't write it here?) #include <iostream>
You didn't specify the namespace for cin, cout and endl (they are in std namespace)
You had a superfluous "[" in the statement a[j][[i], by all likelihood you wanted to write a[j][i]
The compiling code looks like this:
#include <iostream>
const int rows = 2; // Declared As Global Variable
const int cols = 3; // Declared As Global Variable
void getData(int arr[][cols], int rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
std::cout << "Enter Element: ";
std::cin >> arr[i][j];
}
}
}
void printData(int a[][cols], int rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
std::cout << a[i][j] << "\t";
}
std::cout << std::endl;
}
}
void print_single(int a[], int row)
{
for(int i = 0; i < row; i++)
{
std::cout << a[i] << "\t";
}
}
void maxValuesR(int a[][cols], int rv[], int row)
{ int foxi;
for(int i = 0; i < row; i++)
{
foxi = a[i][0];
for(int j = 0; j < cols; j++)
{
if(foxi < a[i][j])
{
foxi = a[i][j];
}
}
rv[i] = foxi;
}
}
void maxValuesC(int a[][cols], int cv[], int row)
{
int maxi;
for(int i = 0; i < cols; i++)
{
maxi = a[0][i];
for(int j = 0; j < row; j++)
{
if(maxi > a[j][i])
{
maxi = a[j][i];
}
}
cv[i] = maxi;
}
}
int main()
{
int rowVector[rows];
int colVector[cols];
int a[rows][cols];
std::cout << "Fill Array_1. " << std::endl;
getData(a, rows);
std::cout << "Array_1." << "\n\n";
printData(a, rows);
std::cout << std::endl;
maxValuesR(a, rowVector, rows);
print_single(rowVector, rows);
std::cout << "\n\n";
maxValuesC(a, colVector, rows);
print_single(colVector, rows);
return 0;
}
Can't tell whether it produces the output you want, though, because you didn't specify any example input (let alone the corresponding expected output)...
I'm trying to create a 3D char array with dynamic memory. I create the char*** point in main then pass it to input and everything works fine until the input function returns and i try to repring the same locaton from main. I get "Access violation reading location." Any suggestions?
void input(char ***a, int f, int n)
{
cin >> f;
cin >> n;
a = new char**[f];
for (int i =0; i <f; ++i)
{
a[i]= new char*[n];
for (int j=0; j <n; ++j)
{
a[i][j] = new char[n];
}
}
for (int i =0; i<f; ++i)
{
for (int j=0; j<n; ++j)
{
for (int k = 0; k <n ;++k)
{
a[i][j][k] = '.';
cout << a[i][j][k];}
}
}
cout <<endl << endl<< a[2][5][5]; //test to see is value is '."
}
int main()
{
char ***station = 0;
int floors=0, n=0;
input(station, floors, n);
cout << endl << endl << station[2][5][5];
}