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;
}
Related
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
}
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>());
My program is crashing when I run it, I ran the debugger and it reports this:
std::out_of_range at memory location 0x0066F6F4.
My code is as follows:
#include <iostream>
#include <string>
int main() {
std::string name = "Alexi";
for (unsigned int i = 0; i <= name.length(); i++) {
for (unsigned int x = 0; x <= i; x++) {
if (i == x) std::cout << name.at(x);
else std::cout << " ";
}
std::cout << '\n';
}
return 0;
}
Any help would be appreciated.
You should have i < name.length() and not i <= name.length()
#include <iostream>
#include <string>
int main() {
std::string name = "Alexi";
for (unsigned int i = 0; i < name.length(); i++) {
for (unsigned int x = 0; x <= i; x++) {
if (i == x) std::cout << name.at(x);
else std::cout << " ";
}
std::cout << '\n';
}
return 0;
}
The string name has the length of 5.
Internally, it is stored in a char [] (character array) called namewhere the
character A is stored at name[0]
character l is stored at name[1]
character e is stored at name[2]
character x is stored at name[3]
character i is stored at name[4]
So note that the length was 5 but your maximum index is 4.
This is because C and C++ arrays are 0-indexed
I am having trouble understanding the output I am getting for this piece of code
#include<iostream>
#include<stdio.h>
using namespace std;
int main() {
int i = 0;
int j = 0;
int k = 0;
char ch[2][14];
char re[2][14];
cout << "\nEnter 1st string \n";
cin.getline(ch[0], 14);
cout << "\nEnter the 2nd string\n";
cin.getline(ch[1], 14);
for(i = 0; i < 2; i++) {
int len = strlen(ch[i]);
for(j = 0, k = len - 1; j < len; j++, k--) {
re[i][j]=ch[i][k];
}
}
cout << "\nReversed strings are \n";
cout << re[0];
cout << endl << re[1] << endl;
return 0;
}
for example
/*
Input :
hello
world
Output :
olleh<some garbage value>dlrow
dlrow
*/
Sorry if it very basic, but I can't understand the reason. Thanks in advance.
Make sure that re[0] and re[1] are null-terminated
For example during initialization you could do
for (int i = 0; i < 14; i++)
{
re[0][i] = '\0';
re[1][i] = '\0';
}
But aside from that I suggest to used std::string and std::reverse and the like.
for (i = 0; i < 2; i++)
{
int len = strlen(ch[i]);
for (j = 0, k = len - 1; j < len; j++, k--)
{
re[i][j] = ch[i][k];
}
re[i][len] = '\0';
}
you have to terminate your reversed strings.
also you should #include <string.h> for the strlen() function.
You forgot about the terminating zero for strings in array re Simply define the array the following way
char ch[2][14] , re[2][14] = {};
^^^^
Also take into account that you should remove header <stdio.h> because it is not used and instead of it include header <cstring>.
This task can be done with using standard algorithm std::reverse_copy
For example
#include <iostream>
#include <algorithm>
#include <cstring>
int main()
{
const size_t N = 2;
const size_t M = 14;
char ch[N][M] = {};
char re[N][M] = {};
std::cout << "\nEnter 1st string: ";
std::cin.getline( ch[0], M );
std::cout << "\nEnter the 2nd string: ";
std::cin.getline( ch[1], M );
std::cout << std::endl;
for ( size_t i = 0; i < N; i++ )
{
std::reverse_copy( ch[i], ch[i] + std::strlen( ch[i] ) , re[i] );
}
for ( const auto &s : re ) std::cout << s << std::endl;
return 0;
}
My professor is forcing me to use a program called RAPTOR, which preety much creates and executes flow charts and psuedocode. And I'm stuck converting my flowchart, insted of writing in the actual language. I finally got my program to compile, but now it wont give me the same results as in the RAPTOR program, and my black1 variable keeps returning 0, where as black2 keeps spitting out a number like 1,000. Anyways any help would be greatly appreciated thank you!
Here's the code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int trials;
int first;
string bag;
int j;
float percent;
string temp;
int black2;
int runs;
int l;
int black1;
int firstdraw;
int c;
string possible;
int seconddraw;
srand(time(NULL));
l = bag.length();
int r = rand() % 4;
possible ="bw";
runs =0;
while (!(runs>9))
{
black1 =0;
black2 =0;
runs =runs+1;
trials =0;
while (!(trials==1000))
{
trials =trials+1;
bag ="bbwww";
l = bag.length();
c =1;
temp =" ";
while (!(c==5))
{
j = r ;
temp[1] = bag[c];
bag[c] = bag[j];
bag[j] = temp[1];
c =c+1;
}
c =1;
j = r;
firstdraw =bag[j];
if (firstdraw==possible[1])
{
black1 = black1+1;
first = 1;
}
else
{
first = 0;
}
while (!(j>l-1))
{
bag[j] = bag[j + 1];
j =j+1;
}
l =l-1;
j = r;
seconddraw = bag[j];
if (seconddraw==possible[1] && first==1)
{
black2 =black2+1;
}
else
{
}
}
percent = 100.0 * black2/black1;
cout << black2 << " " << black1 << endl;
cout << "Percentage for run #" << runs << " and " << trials << " trials: %" << percent << endl; }
return 0;
}
I'm sorry but I won't fix your whole code, I made it more readable, removed that ugly using namespace std; and left some comments for you. Check this out, fix your code, and take a look into some tutorials if needed. I've gotten a debug assertion over and over again with this code. Guessing you never runned any debugger and just had Release mode on.
Also don't forget to initialize the variables. You risk undefined behaviour.
These two strings walk into a bar and sit down. The bartender says, "So what'll it be?"
The first string says, "I think I'll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu"
"Please excuse my friend," the second string says, "He isn't null-terminated."
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <time.h>
#include <stdlib.h>
int main()
{
int trials = 0;
int first = 0;
int c = 0;
int j = 0;
int l = 0;
int black2 = 0;
int runs = 0;
int black1 = 0;
int firstdraw = 0;
int seconddraw = 0;
float percent = 0.0f;
std::string temp = "\0";
std::string bag = "\0";
std::string possible = "\0";
srand (time(NULL));
l = bag.length();
fflush(stdin); // You may want to do this to get random numbers from rand() otherwise it may use the same number over and over again
int r = 0;
r = rand() % 4;
possible = "bw";
runs = 0;
while (!(runs > 9))
{
black1 = 0;
black2 = 0;
runs = runs + 1; //Why no for loop ?
trials = 0;
while (!(trials == 1000))
{
trials = trials + 1; //Why no for loop ?
bag ="bbwww";
l = bag.length();
c = 1;
temp =" ";
while (!(c == 5))
{
j = r;
temp[1] = bag[c]; // temp[1] <- debug assertion, can't work
bag[c] = bag[j];
bag[j] = temp[1]; // temp[1] <- debug assertion, can't work either
c = c + 1;
}
c = 1;
j = r;
firstdraw = bag[j];
if (firstdraw == possible[1])
{
black1 = black1 + 1;
first = 1;
}
else
{
first = 0;
}
while (!(j > l - 1))
{
bag[j] = bag[j + 1]; //Will most likely cause a debug assertion too...
j = j + 1;
}
l = l - 1;
j = r;
seconddraw = bag[j];
if (seconddraw == possible[1] && first==1)
{
black2 = black2 + 1;
}
else
{
//Not needed !?!
}
}
percent = 100.0 * black2 / black1;
std::cout << black2 << " " << black1 << std::endl;
std::cout << "Percentage for run #" << runs << " and " << trials << " trials: %" << percent << std::endl;
}
return 0;
}