I have to use a dynamic array for this project but I can't pass it by reference, I am not sure what to do, I initialized the 2d dynamic array, but not sure how to pass it by reference.
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
#define row 5
#define col 14
typedef string* StrArrPtr;
void set_up_array(string (&&layout_array)[row][col]);
int main()
{
StrArrPtr *layout_array = new StrArrPtr[col];
for(int i = 0; i < row; i++)
{
layout_array[i] = new string[col];
}
//string layout_array[row][col];
set_up_array(layout_array);
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if(j == 1 && i > 0)
{
cout << right << setw(11);
}
cout << "| " << layout_array[i][j] << " ";
}
cout << "|" << endl;
}
return 0;
}
void set_up_array(string (&&layout_array)[row][col])
{
layout_array[0][0] = "Lab Number"; //First Column / first row
layout_array[1][0] = "1"; // second row
layout_array[2][0] = "2"; // third row
layout_array[3][0] = "3"; // fourth row
layout_array[4][0] = "4"; // fifth row
}
I am new to c++ so the solution might be very obvious but I just can't see it. Any help would be appreciated.
Given what you are doing with the array in set_up_array, you don't need to pass the actual string* by reference, because you're simply dereferencing it (telling your computer where to look for a string) and altering the value of string located at that index. When you pass an array in C/C++, you're just passing in an int, so don't worry about references unless you're modifying what the pointer is pointing to.
(It would be like string * &ref)
To pass a 2D array by reference, use:
void set_up_array(string (&layout_array)[row][col]);
In order to be able call that function, the variable needs to be of type string [row][col].
Change main to:
int main()
{
std::string layout_array[row][col];
set_up_array(layout_array);
...
}
Since you know the sizes of the arrays at compile time, it will be better to use std::array.
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <iomanip>
using namespace std;
const int row = 5;
const int col = 14;
void set_up_array(std::array<std::array<std::string, col>, row>& layout_array);
int main()
{
std::array<std::array<std::string, col>, row> layout_array;
set_up_array(layout_array);
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if(j == 1 && i > 0)
{
cout << right << setw(11);
}
cout << "| " << layout_array[i][j] << " ";
}
cout << "|" << endl;
}
return 0;
}
void set_up_array(std::array<std::array<std::string, col>, row>& layout_array)
{
layout_array[0][0] = "Lab Number"; //First Column / first row
layout_array[1][0] = "1"; // second row
layout_array[2][0] = "2"; // third row
layout_array[3][0] = "3"; // fourth row
layout_array[4][0] = "4"; // fifth row
}
Arrays are always pass by reference.. your problem is how to pass a 2d array of string...
analyze how i did it:
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
#define row 5
#define col 14
typedef string* StrArrPtr;
void set_up_array(string* layout_array[]);
int main()
{
StrArrPtr *layout_array = new StrArrPtr[row];
for(int i = 0; i < row; i++)
{
layout_array[i] = new string[col];
}
//string layout_array[row][col];
set_up_array(layout_array);
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if(j == 1 && i > 0)
{
cout << right << setw(11);
}
cout << "| " << layout_array[i][j] << " ";
}
cout << "|" << endl;
}
return 0;
}
void set_up_array(string* layout_array[])
{
layout_array[0][0] = "Lab Number"; //First Column / first row
layout_array[1][0] = "1"; // second row
layout_array[2][0] = "2"; // third row
layout_array[3][0] = "3"; // fourth row
layout_array[4][0] = "4"; // fifth row
}
Related
Program stop occur in this line
guess = secret;
From that, I guess that reference is broken, because if I change reference to simple value
const string secret = word_list[idx_word];
the program finishes correctly. So, my question is why this happen. The word_list is not changed/resided in loop.
Erorr occur on 392 iteration.
#include <QCoreApplication>
#include <QFile>
#include <QDir>
#include <QVector>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <thread>
#include <mutex>
#include <iomanip>
#include <string>
using namespace std;
bool debug = true;
const int COUNT = 5;
string MASK_FULL_MATCH(COUNT, 'o');
const string getMask(const string& word, const string& answer) {
if (word.size() != COUNT || answer.size() != COUNT) {
cout << word.size() << " " << answer.size() << endl;
}
char mask[5];
bool visited[5];
for (int i = 0; i < COUNT; i++) {
mask[i] = 'x';
visited[i] = false;
}
// find correct letters
for(int i = 0; i < COUNT; i++){
if (word[i] == answer[i]){
mask[i] = 'o';
visited[i] = true;
}
}
// find present letters
for (int i = 0; i < COUNT; i++){
if (mask[i] != 'o'){
for (int j = 0; j < COUNT; j++) {
if (answer[j] == word[i] && !visited[j]) {
mask[i] = '-';
visited[j] = true;
break;
}
}
}
}
return string(mask, COUNT);
}
int main(int argc, char *argv[])
{
QString pathToFile = QString("C:/Users/Ivan/Desktop/w_assets/w") + QString::number(COUNT) + QString("_entropy.txt");
QFile file(pathToFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return -2;
QTextStream in(&file);
QVector<string> word_list;
while (!in.atEnd()) {
QString line = in.readLine();
word_list.append(line.split(QChar(' '))[0].toStdString());
}
file.close();
for (int idx_word = 0; idx_word < word_list.size(); idx_word++) {
const string &secret = word_list[idx_word];
cout << secret << '\t';
}
int total = 0;
for (int idx_word = 0; idx_word < word_list.size(); idx_word++) {
const string &secret = word_list[idx_word];
cout << "NEW SECRET " << secret << endl;
QVector<string> possible_answers = word_list;
for (int row = 0; row < 6; row++) {
string guess;
if (row == 0) {
guess = word_list[0];
}
else {
cout << "before broken secret\n";
guess = secret;
cout << "after broken secret\n";
cout << "row " << row << "; GUESS " << guess << endl;
}
debug = true;
string mask = getMask(guess, secret);
debug = false;
cout << "MASK: " << mask << endl;
if (mask == MASK_FULL_MATCH) {
break;
}
QVector<string> new_possible_answers;
for (const auto& pa : possible_answers) {
if (getMask(guess, pa) == mask) {
new_possible_answers.append(pa);
}
}
possible_answers = new_possible_answers;
cout << "NEW POSSIBLE WORDS SIZE " << possible_answers.size() << endl;
}
}
return 0;
}
word_list[0]; - this is a non-const operation in a QVector (see documentation, there is even a note about the possible detach) and since the reference count of your word_list is two due to the copy to possible_answers some lines above, the container has to do a detach and therefore your reference goes out of scope.
If you work with references on Qt containers you have to make sure to either have a reference count of 1 or only use const-access to the container (e.g. by creating a const ref to the container -> const auto &const_word_list = word_list; guess = const_word_list [0])
I have a function that changes the values of 4 array elements. It does that correctly but it also makes a copy of the 4 elements with an offset of (1, -10). I know it doesn't make much sense and I'm not sure how to explain it so I'll just show it.
Here is the code:
void PlayArea::addFigure(std::array<Vector2i, 4> figure, int color)
{
printCells();
std::cout << "\n\n";
for (int i = 0; i < 4; i++)
{
int x = figure[i].x;
int y = figure[i].y;
cell[x][y] = color;
}
printCells();
std::cout << "\n\n";
}
Notice the printCells function. What it does is print the elements of the array. The one before the for loop prints all zeros. The one after the for loop prints something like the one I described in the first paragraph. Here's a picture:
Edit:
The printCells method prints the contents of the 10 by 20 array cell, nothing more. I just added it for debugging. Here it is:
void PlayArea::printCells()
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 10; j++)
{
std::cout << cell[j][i] << ", ";
}
std::cout << "\n";
}
}
The addFigure method is supposed to only add 4 elements to the cell array. I can't figure out how it stores 8.
Edit 2:
I tried replicating the problem on a separate project and this is the code:
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <array>
#include <iostream>
#include <conio.h>
using namespace sf;
int cell[20][10] = { 0 };
void printCells()
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 10; j++)
{
std::cout << cell[j][i] << ", ";
}
std::cout << "\n";
}
}
int main()
{
std::array<Vector2i, 4> figure;
figure[0].x = 1;
figure[0].y = 16;
figure[1].x = 1;
figure[1].y = 17;
figure[2].x = 1;
figure[2].y = 18;
figure[3].x = 1;
figure[3].y = 19;
printCells();
std::cout << "\n\n";
for (int i = 0; i < 4; i++)
{
cell[figure[i].x][figure[i].y] = 7;
}
//cell[1][19] = 7;
printCells();
std::cout << "\n\n";
_getch();
}
Every time I change a value of an element of the array it still changes two values 1 column and 10 rows apart. Here is an image:
I'm trying to sort my array pairs by int, but my sort is saying 'unable to resolve identifier' to pairs.begin(), pairs.end(), and compare_pairs_second(). I cannot figure out why but i'm probably doing something wrong?
Here is my code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <iterator>
using namespace std;
main()
{
string name[10];//declaring an array name
int number[10];//declaring an array number
cout << "Please input 10 names \n";//output
for(int i = 0; i < 10; i++){//for statement
cin >> name[i];//inputting names in the array names
}
cout << "Please input their corresponding numbers \n";//output
for(int x = 0; x < 10; x++){
cin >> number[x];//inputting numbers
}//end for
int i = 0;//redeclaring i to be 0
int x = 0;//redeclaring x to be 0
for(int l = 0; l < 10; l++)//for statement
{
cout << name[i] << ": " << number[x] << "\n";
i++;//adding 1 to i so outputs all of array name
x++;//adding 1 to x so outputs all of array number
}//end for
pair<string, int> pairs[10];
int i = 0;
int x = 0;
for(int z = 0; z < 10; z++)
{
pairs[z] = make_pair(name[i], number[x]);
i++;
x++;
}
std::sort(pairs.begin(), pairs.end(), compare_pairs_second<std::less>());
int i = 0;
int x = 0;
for(int z = 0; z < 10; z++)
{
name[i] = pairs[z].first;
number[x] = pairs[z].second;
i++;
x++;
}
string search = "";
cout << "Enter a name to search for";
cin >> search;
size_t found = pairs.find(search);
if(found!=string::npos)
cout << search << pairs;
} //end main
UPDATE/EDIT:
I got my sort to work...to an extent. I deleted the line of code that was giving me errors, and it now sorts my number array, which is half of what I wanted. But now how do I keep the names with their respective numbers without using my pairs variable that I had prior to this fix?
Ex
array number[5]={5, 2, 9, 11, 27};
array name[5]={"Steve", "John", "Bob", "Larry", "Patric"};
Output after sorting:
John: 2 Steve: 5 Bob: 9 Larry: 11 Patric: 27
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <iterator>
using namespace std;
main()
{
const int size = 10;
string name[10];//declaring an array name
int number[10];//declaring an array number
cout << "Please input 10 names \n";//output
for(int i = 0; i < 10; i++){//for statement
cin >> name[i];//inputting names in the array names
}
cout << "Please input their corresponding numbers \n";//output
for(int i = 0; i < 10; i++){
cin >> number[i];//inputting numbers
}//end for
sort(number,number+size);
int i;
int j;
int min;
int counter = 0;
for(i = 0; i < counter; i++)
{
min = i;
for(j = i+1; j < counter; j++)
{
if(name[j] < name[min])
{
string tempString = name[i];
name[i] = name[j];
name[j] = tempString;
int tempInt = number[i];
number[i] = number[j];
number[j] = tempInt;
}
}
}
for(i = 0; i < 10; i++)
{
cout << name[i] << ": " << number[i] << "\n";
};
} //end main
Current output sorts numbers but does not keep names with them.
pairs is raw array, not a STL container; you can't invoke method on it like pairs.begin() and pairs.end().
Since C++11 you could use std::begin() and std::end(), which are overloaded for supporting raw arrays. e.g.
std::sort(std::begin(pairs), std::end(pairs), compare_pairs_second<std::less>());
I am relative new to C++ so please have mercy. I am trying to cout the start and end of a "vector" in a 2D array 40*20. but the "b" dont get placed in with the last 2 for loops. In the end i want to display 2 vectors with the second starting at [0][0] and going the same direction as the one typed in by the user. I also don't know how to, get the b's to be from the starting point of the line to the end point of the line and not just start and ending point. But the main problem is that the x is not changed with the b.
Thanks for your help.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int vectorsx;
int vectorsy;
int vectorendx;
int vectorendy;
char display[40][40];
char row;
char column;
typedef struct
{
double x;
double y;
}punkt;
typedef struct
{
punkt PA;
punkt PE;
}vector;
void query()
{
cout << "Startpunkt x:";
cin >> vectorsx;
cout << "Startpunkt y:";
cin >> vectorsy;
cout << "Endpunkt x:";
cin >> vectorendx;
cout << "Endpunkt y:";
cin >> vectorendy;
}
int main()
{
query();
vector v;
v.PA.x = vectorsx;
v.PA.y = vectorsy;
v.PE.x = vectorendx;
v.PE.y = vectorendy;
vector v2;
v2.PA.x = 0;
v2.PA.y = 0;
v2.PE.x = v.PE.x - v.PA.x;
v2.PE.y = v.PE.y - v.PA.y;
for (int row = 0; row < 20; row++)
{
for (int column = 0; column < 40; column++)
{
display[row][column] = 'x';
}
}
for (int row = 0; row < 20; row++)
{
for (int column = 0; column < 40; column++)
{
cout << display[row][column];
}
cout << endl;
}
for (row = 0; row < 20; row++)
{
for(column = 0; column < 40;column++)
{
display[vectorsx][vectorsy] = 'b';
}
}
for (row = 0; row < 20; row++)
{
for (column = 0; column < 40; column++)
{
display[vectorendx][vectorendy] = 'b';
}
}
system("Pause");
return 0;
}
You set all elements of display to 'x'. Then you print the contents of dispaly. And then you change the contents of two elements to b. You don't print the array again.
Do the printing after you change the arrays.
You also don't need the loops for the changes, you are changing the same element 800 times.
I am pretty newish at c++ and was wondering how I can place values randomly into an int array.What I want to do is use this function to place into a grid randomly for the purpose of creating a memory matching game.
#include "stdafx.h"
#define NOMINMAX
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <time.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void shuffleL(int [][8]);
int main()
{
//shuffles characters
shuffleS(charactersS);
for (int i = 0; i <= 8; i++)
{
cout << "---";
}
cout << endl;
//output grid
for (int r = 0; r < 4; r++)
{
cout << r + 1 << " | ";
for (int c = 0; c < 4; c++)
{
cout << " [VGC] ";
status[r][c] = false;
}
cout << endl;
}
cout << endl;
}
void shuffleL(int characters[][8])
{
string vgc[50] = { "PacMan", "Laura", "Mario", "Sonic", "Link", "Snake", "Drake", "Samus", "MegaMan", "Kratos",
"Isaac", "DK", "Dante", "Crash", "Spyro", "Kirby", "Ryu", "Yoshi", "Sora", "Strider",
"DigDug", "Lil_Mac", "Pit", "Booker", "Rayman", "Frogger", "Marcus", "Shepard", "Sly", "Ezio",
"Guybrush", "Leon", "Raz", "Ninten", "Ralph", "Crono", "MaxPayne", "Fox", "Simon", "Cole",
"Pheonix", "Corvo", "Parappa", "Faith", "Lucas", "Scorpion", "Gordon", "Roland", "Chell", "Olimar" };
string temp;
for (int s = 0; s <= 4; s++)
{
for (int x = 0; x<16; x++)
{
srand((unsigned)time(NULL));
int i = rand() % 15 + 1;
temp = vgc[x];
vgc[x] = vgc[i];
vgc[i] = temp;
}
}
int i = 0;
//Input of Values in Here
for (int r = 0; r < 50; r++)
{
for (int c = 0; c < 50; c++)
{
characters[r][c] = vgc[i]; //THIS VGC GIVES ME THE ERROR
cout << characters[r][c];
i = i + 1;
}
cout << endl;
}
}
It also gives me an error for one of the variableNames (vgc) saying
1 IntelliSense: no suitable conversion function from "std::string" to "int" exist
I am completely stumped on how to fix this solution.
Dont do int characters [] [8]; , but string characters [] [8]; . The type you specify at the start of the array declaration is the type of info the array will store.
Peace.
The problem is, you are trying to store string in your int type array.
for (int c = 0; c < 50; c++)
{
characters[r][c] = vgc[i]; //<- **Here**
cout << characters[r][c];
i = i + 1;
}