i have a problem on this code
the errors says ISO c++ forbids comparison between pointer and integer
here is the exercise problem
-the answers to a true false test are as follows TTFFT given a two dimensional answer array in which each row corresponds to the answers provided on one test, write a function that accepts the two dimensional array and the number of tests as parameters and returns a one dimensional array containing the grades for each test.(each question is worth 5 points so that the maxinum possible grade is 25)
any answers will help.
this is the code
#include <iostream>
using namespace std;
int numberofgrades(char [][5], int []);
int main()
{
char testanswers[5][5] = {0};
int testgrades[5] = {0};
int counting1, counting2, counting3;
counting1 = 1;
counting2 = 1;
counting3 = 1;
cout << "this program will record the testgrades you entered: "
<< endl;
for(int i = 0; i < 5; i++)
{
for(int k = 0; k < 5; k++)
{
cout << "enter answers for test no. " << counting1 << ": ";
cin >> testanswers[i][k];
}
counting1++;
}
numberofgrades(testanswers,testgrades);
cout << "testing..." << endl;
cout << endl;
for(int o = 0; o < 5; o++)
{
cout << "test no. " << counting2;
for(int l = 0; l < 5; l++)
{
cout << " " << testanswers[o][l];
}
counting2++;
cout << endl;
}
cout << "testing...." << endl;
cout << endl;
cout << "the total number of scores per test no. is: "
<< endl;
for(int t = 0; t < 5; t++)
{
cout << "test no. " << t << ": " << testgrades[t] << endl;
}
return 0;
}
int numberofgrades(char t1a [][5], int tg1[])
{
for(int j = 0; j < 5; j++)
{
for(int i = 0; i < 5; i++)
{
if(t1a[i] == 't') //this is the problem
{
tg1[j] = tg1[j] + 5;
}
else if(t1a[i] == 'T')
{
tg1[j] = tg1[j] + 5;
}
else if(t1a[i] == 'f')
{
tg1[j] = tg1[j] + 0;
}
else if(t1a[i] == 'F')
{
tg1[j] = tg1[j] + 0;
}
else{
cout << "invalid letter exiting the program"
<< endl;
return 0;
}
}
}
return tg1[5];
}
Related
I am working on this university project and I have 2-3 issues. I want to get them done 1 by 1.
My first question is about one of my getter/setter functions.
Here are the class's attributes:
I got getters and setters for each attribute of the class, and a view function that shows the attributes of a class object
class Game{
private:
string gameName;
float gamePrice;
int gameNoPlatforms;
string *gamePlatforms;
int gameNoSitesRating;
int *gameRatings;
int gameSales[19];
string gameLaunchers[5];
}
Those 2 are my get/set function for the atribute * string gamePlatforms:
void setGamePlatforms(string *newGamePlatforms, int newGameNoPlatforms)
{
if (this->gamePlatforms != NULL)
{
delete[] this->gamePlatforms;
}
if (newGameNoPlatforms > 0 )
{
this->gameNoPlatforms = newGameNoPlatforms;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
this->gamePlatforms[i] = newGamePlatforms[i];
}
}
else cout << "The number of new game platforms can't be negative. "<< endl;
}
string *getGamePlatforms()
{
return this->gamePlatforms;
}
And this is the input that i tried to test on:
int main(){
Game g1;
int noPlatforms = 10;
string*model = new string[noPlatforms];
for (int i = 0; i < noPlatforms; i++)
{
model[i] = "Platform " + to_string(i+10);
}
g1.setGamePlatforms(model, noPlatforms); //-> not working
g1.setGamePlatforms(model, noPlatforms);
cout << g1.getGamePlatforms();
return 0;
}
And for me it is returning a weird value. I think it is an address. What have I done wrong?
Edit: the entire class:
class Game{
private: string gameName;
float gamePrice;
int gameNoPlatforms;
string *gamePlatforms;
int gameNoSitesRating;
int *gameRatings;
int gameSales[19];
string gameLaunchers[5];
public:
// Constructor1 fara parametrii pt game
Game():gameReleaseYear(2000)
{
this->gameName = "Counter-Strike";
this->gamePrice = 20;
this->gameNoPlatforms = 10;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
this->gamePlatforms[i] = "Platform" + to_string(i+1);
}
this->gameNoSitesRating = 5;
this->gameRatings = new int[this->gameNoSitesRating];
for (int i = 0; i < this->gameNoSitesRating; i++)
this->gameRatings[i] = i + 1;
for (int i = 1; i < 19; i++)
{
this->gameSales[i] = i + 2;
}
for (int i = 1; i < 5; i++)
{
this->gameLaunchers[i] = "Launcher" + to_string(i);
}
this->noGames++; //incrementare nr games
}
// Constructor cu parametrii pt game
Game(string gameNameP, float gamePriceP, int gameNoPlatformsP, string *gamePlatformsP, int gameNoSitesRatingP, int *gameRatingsP, int gameSalesP[19], string gameLaunchersP[5]) :gameReleaseYear(2005)
{
this->gameName = gameNameP;
this->gamePrice = gamePriceP;
this->gameNoPlatforms = gameNoPlatformsP;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
gamePlatforms[i] = gamePlatformsP[i];
}
this->gameNoSitesRating = gameNoSitesRatingP;
this->gameRatings = new int[this->gameNoSitesRating];
for (int i = 0; i < this->gameNoSitesRating; i++)
{
gameRatings[i] = gameRatingsP[i];
}
for (int i = 0; i < 19; i++)
{
gameSales[i] = gameSalesP[i];
}
for (int i = 0; i < 5; i++)
{
gameLaunchers[i] = gameLaunchersP[i];
}
}
// Destructor pt game
~Game()
{
if (this->gamePlatforms != NULL)
delete[] this->gamePlatforms;
if (this->gameRatings != NULL)
delete[] this->gameRatings;
noGames--; // decrementare nr games
}
// Functie de afisare pentru game
void view()
{
cout << "For the game: " <<' '<< this->gameName << " we have the following details: " << endl;
cout << endl;
cout << "The release year for the game was: " << this->gameReleaseYear << endl;
cout << endl;
cout << "The game is sold at a full price of: " << this->gamePrice << " euroes" << endl;
cout << endl;
cout << "The number of sites that are rating this game is: " << this -> gameNoSitesRating << " and the ratings are: " << endl;
cout << endl;
for (int i = 0; i < this->gameNoSitesRating; i++)
{ if(this->gameRatings[i]+i >10)
cout << "The no " << i + 1 << " site is rating the game as " << 10 << " stars out of 10 " << endl;
else
cout << "The no " << i+1 << " site is rating the game as " << this->gameRatings[i] << " stars out of 10 " << endl;
}
cout << endl;
cout << "The sales of the game from the release year since now are: " << endl;
for (int i = 1; i < 19; i++)
{
cout << "For the year " << i << " the sales estimate at " << gameSales[i] << " millions" << endl;
}
cout << endl;
cout << "The launchers that support the game are: " << endl;
for (int i = 1; i < 5; i++)
{
cout << gameLaunchers[i] << ' ' << endl;
}
cout << "The game is currently running on " << this->gameNoPlatforms << " number of platforms, and those platforms are: " << endl;
cout << endl;
for (int i = 0; i < this->gameNoPlatforms; i++)
{
cout << this->gamePlatforms[i] << endl;
}
}
// functiile accesor getters
string getGameName()
{
return this->gameName;
}
float getGamePrice()
{
return this->gamePrice;
}
int getGameNoPlatforms()
{
return this->gameNoPlatforms;
}
int getNoSitesRating()
{
return this->gameNoSitesRating;
}
string *getGamePlatforms()
{
return this->gamePlatforms;
}
int *getGameRatings()
{
return this->gameRatings;
}
string *getGameLaunchers()
{
return this->gameLaunchers;
}
//functiile accesor setters
void setGameName(string newGameName) //testat pt input valid si invalid
{ if(newGameName != gameName)
this->gameName = newGameName;
else cout << "This is the actual name of the game, no modify is required.";
}
void setGamePrice(float newPrice) //testat pt input valid si invalid;
{ if(newPrice>0)
this->gamePrice = newPrice;
else cout << "The price can't be negative." << endl;
}
void setGameNoPlatforms(int newNoPlatforms) //testat pentru input valid, input mai mare ca 5 si negativ
{ if(newNoPlatforms>0 && newNoPlatforms <5)
this->gameNoPlatforms = newNoPlatforms;
else cout << "The number of platforms can't be negative and must be less than 5, since this is the maximum number of existing platforms." << endl;
}
void setGameNoSitesRating(int newNoSites) //testat pentru input valid, input negativ, input mai mare decat 15
{ if(newNoSites>0 && newNoSites<15)
this->gameNoSitesRating = newNoSites;
else cout << "The number of sites can't be negative nor greater than 15 since this is the maximum number of sites that our game is rated on." << endl;
}
void setGamePlatforms(string *newGamePlatforms, int newGameNoPlatforms) //testat pt input valid, returneaza adresa
{
if (this->gamePlatforms != NULL)
{
delete[] this->gamePlatforms;
}
if (newGameNoPlatforms > 0 ){
this->gameNoPlatforms = newGameNoPlatforms;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
this->gamePlatforms[i] = newGamePlatforms[i];
}
}
else cout << "The number of new game platforms can't be negative. "<< endl;
}
void setGameRatings(int *newGameRatings, int newNoRatings) //testat pt valori valide, testat pt neNoRatings negativ;
{
if (newNoRatings < 0)
cout << "The new number of sites that are rating the game can't be negative";
else {
if (this->gameRatings != NULL)
{
delete[] this->gameRatings;
}
this->gameNoSitesRating = newNoRatings;
this->gameRatings = new int[this->gameNoSitesRating];
for (int i = 0; i < this->gameNoSitesRating; i++)
{
this->gameRatings[i] = newGameRatings[i];
}
}
}
void setGamesSales(int *newSales)
{
if (newSales != NULL)
{
for (int i = 0; i < 19; i++)
{
gameSales[i] = newSales[i];
}
}
else cout << "The sales can't be null." << endl;
}
void setLaucnhers(string *newLaunchers)
{
if (newLaunchers != NULL)
{
for (int i = 0; i < 5; i++)
{
gameLaunchers[i] = newLaunchers[i];
}
}
else cout << "The name of the new launchers can't be null." << endl;
}
//variabile static/const
const int gameReleaseYear;
static int noGames;
};
int Game::noGames = 0;
Try this:
int Game::getNumberOfPlatforms() const
{
return gameNoPlatforms;
}
And print them:
for(int i = 0; i < g1.getNumberOfPlatforms(); ++i)
cout << g1.getGamePlatforms()[i] << endl;
I used the following code for palindrome check of an integer array and used the value of variable 'declare' as check of palindrome. I used the technique that if declare is 1 at the end, array is palindrome, else not. But its not working. In the end of code, it always keeps the value of declare which was initialized, independent of rest of the code. Please Debug.
#include <iostream>
using namespace std;
void main()
{
int array1[3] = {0,0,1};
int j = 2;
cout << "Given Array is:\n";
for (int i = 0; i < 3; i++)
cout << array1[i];
cout << endl;
int determiner[3];
for (int i = 0; i <3; i++){
determiner[j] = array1[i];
j -= 1;
}
cout << "Reversed Array is:\n";
for (int i = 0; i < 3; i++)
cout << determiner[i];
cout << endl;
int declare;
for (int u = 0; u < 3; u++)
{
if (array1[u] = determiner[u])
{
declare = 1;
}
if (array1[u] != determiner[u])
{
declare = 0;
break;
}
}
cout << endl;
cout << declare<< endl;
if (declare==1)
cout << "Given Array is Palindrome. Cheers!!!\n";
if (declare==0)
cout << "Emhmm! This aint Palindrome.\n";
system("pause");
}
if (array1[u] = determiner[u])
should be
if (array1[u] == determiner[u])
I'm having issues with the final implementation of merge sort. Below is my code:
#include <iostream>
using namespace std;
void decomposeArray(int* array, int length) {
int arrayOfArrays[length];
cout << endl;
for(int i = 0; i < length; i++) {
cout << "Array: " << array[i] << endl;
}
if (length > 1) {
int midpoint = length/2;
int elemInFirst = midpoint;
int elemInSecond = elemInFirst;
if ((length % 2) == 1) {
elemInSecond += 1;
}
int firstArray[elemInFirst];
int secondArray[elemInSecond];
for(int i = 0; i < elemInFirst; i ++) {
firstArray[i] = array[i];
}
for(int j = elemInFirst; j < length; j++) {
secondArray[j-elemInFirst] = array[j];
}
for(int i = 0; i < elemInFirst; i++) {
cout << "First half: " << firstArray[i] << endl;
}
for(int i = elemInFirst; i < length; i++) {
cout << "Second half: " << secondArray[i-elemInFirst] << endl;
}
decomposeArray(firstArray, elemInFirst);
decomposeArray(secondArray, elemInSecond);
int* superiorArray;
int* inferiorArray;
int dominantIndex;
int inferiorIndex;
if (elemInFirst > elemInSecond) {
dominantIndex = elemInFirst;
superiorArray = firstArray;
inferiorIndex = elemInSecond;
inferiorArray = secondArray;
}
else {
dominantIndex = elemInSecond;
superiorArray = secondArray;
inferiorIndex = elemInFirst;
inferiorArray = firstArray;
}
for(int i = 0; i < dominantIndex; i++) {
for (int j = 0; j < inferiorIndex; j++) {
if (superiorArray[i] > inferiorArray[j]) {
int tempVal = array[i];
array[i] = inferiorArray[j];
array[(dominantIndex+j)] = tempVal;
}
else if (j == (inferiorIndex-1)){
int tempVal = array[i];
cout << "Array at i is " << array[i] << endl;
cout << "i is " << i << endl;
cout << "This temp value is " << array[i] << endl;
array[i] = superiorArray[i];
cout << "superiorArray[i]: " << superiorArray[i] << endl;
array[((dominantIndex)+j)] = tempVal;
}
else {
continue;
}
}
cout << "Current array " << endl;
for(int i = 0; i < length; i++) {
cout << array[i] << ",";
}
cout << endl;
}
/*while ((firstIterator != elemInFirst) && (secondIterator !=elemInSecond) && (origArrayIter != length)) {
if (firstArray[firstIterator] > secondArray[secondIterator]) {
array[origArrayIter] = secondArray[secondIterator];
array[origArrayIter + 1] = firstArray[firstIterator];
firstIterator++;
secondIterator++;
origArrayIter+= 2;
}
else {
array[origArrayIter] = firstArray[firstIterator];
array[origArrayIter+1] = secondArray[secondIterator];
firstIterator++;
secondIterator++;
origArrayIter+= 2;
}
}*/
cout << endl;
for(int i = 0; i < length; i++) {
cout << "Array coming out: " << array[i] << endl;
}
/*int tempArray[length];
for(int i = 0; i < length; i++) {
tempArray[i] = array[i];
}
return tempArray;*/
}
}
Specifically, the snippet that I'm having trouble with is merging the arrays back together:
for(int i = 0; i < dominantIndex; i++) {
for (int j = 0; j < inferiorIndex; j++) {
if (superiorArray[i] > inferiorArray[j]) {
int tempVal = array[i];
array[i] = inferiorArray[j];
array[(dominantIndex+j)] = tempVal;
}
else if (j == (inferiorIndex-1)){
int tempVal = array[i];
cout << "Array at i is " << array[i] << endl;
cout << "i is " << i << endl;
cout << "This temp value is " << array[i] << endl;
array[i] = superiorArray[i];
cout << "superiorArray[i]: " << superiorArray[i] << endl;
array[((dominantIndex)+j)] = tempVal;
}
else {
continue;
}
}
cout << "Current array " << endl;
for(int i = 0; i < length; i++) {
cout << array[i] << ",";
}
cout << endl;
}
When I use an array of values {5,3,8,2}, I am returned an array {2,5,3,3}. The step before the final array produces an array {2,3,8,5}. For some reason, I just don't know why the 5 and 8 aren't swapping. Any help or guidance would be appreciated, thank you.
I am having a problem with incrementing a single item in an array. It ends up incrementing another array.. How does it do this? this is what I have:
string simulateFIFO(darray<int> requests, int frameSize) {
string results;
int currentPages[frameSize];
int timer[frameSize];
int hits = 0;
int faults = 0;
cout << "STARTING FIFO" << endl;
for (int i = 0; i < requests.size(); i++) {
cout << requests[i] << " ";
}
cout << endl;
cout << endl;
for (int i = 0; i < frameSize; i++) {
currentPages[i] = -1;
timer[i] = 0;
}
// Start Calculations
for (int i = 0; i < requests.size(); i++) {
bool requestSet = false;
for (int j = 0; j < frameSize; j++) {
if (currentPages[j] < 0) {
// Current frame does not have a page
cout << "FRAME IS EMPTY" << endl;
currentPages[j] = requests[i];
requestSet = true;
faults++;
j = frameSize;
}
else if (currentPages[j] == requests[i]) {
cout << "FRAME IS SAME AS REQUEST" << endl;
// Current Frame is set the the page being requested
timer[j] = 0;
requestSet = true;
hits++;
j = frameSize;
}
cout << currentPages[j] << endl;
timer[j]++;
cout << currentPages[j] << endl;
}
// Check to see if a request was set or not
if (requestSet == false) {
cout << "FRAME NEEDS REPLACED" << endl;
// The request wasnt set. Determine which frame to replace with the new request
int lastUsed = 0;
int lastUsedIndex = 0;
for (int j = 0; j < frameSize; j++) {
if (timer[j] > lastUsed) {
lastUsed = timer[j];
lastUsedIndex = j;
}
}
currentPages[lastUsedIndex] = requests[i];
//timer[lastUsedIndex] = 0;
faults++;
}
cout << "HEY 3: " << currentPages[0] << endl;
cout << "NEW FRAME: ";
for (int j = 0; j < frameSize; j++) {
cout << currentPages[j] << " ";
}
cout << endl;
cout << endl;
}
cout << "FIFO" << endl;
cout << faults << endl;
cout << hits << endl;
cout << endl;
return results;
}
My Output ends up being
0
1
Why does increasing one array actually increase the other as well?
Your code includes a possible path of execution:
j = frameSize;
followed by
timer[j]++;
This accesses out of bounds: for an array of dimension frameSize, the valid indices are 0 through frameSize-1.
I guess you actually want to exit the loop; if so then replace j = frameSize; with break; .
NB. int timer[frameSize]; is not permitted in ISO C++; array bounds must be known at compile-time. Your code is relying on a compiler extension.
I'm having trouble generating and inserting numbers inside the Board. Here is what I have so far:
enter code here
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
bool Valid_Set(int line[])
{
bool found = true;
for (int t = 1; (t <= 9 && found); ++t)
{
found = false;
for (int i = 0; i < 9; ++i)
if (line[i] == t) found = true;
}
return found; // returns true if all value 1-9 are in array.
}
bool Valid_Matrix(int sud[9][9])
{
int i, j, check[9];
bool valid = true;
// check each row
for (j = 0; (j < 9) && valid; ++j)
{
for (i = 0; i < 9; ++i)
check[i] = sud[i][j];
valid = Valid_Set(check);
}
// check each column
for (j = 0; (j < 9) && valid; ++j)
{
for (i = 0; i < 9; ++i)
check[i] = sud[j][i];
valid = Valid_Set(check);
}
// check 3x3 area
for (i = 0; (i < 9) && valid; i += 3)
{
for (j = 0; (j < 9) && valid; j += 3)
{
int t = 0;
for (int x = 0; x < 3; ++x)
for (int y = 0; y < 3; ++y)
check[t++] = sud[x + i][y + j];
valid = Valid_Set(check);
}
}
return valid;
}
void numGenerator(int A[]){
for (int i=0; i<9; i++){
A[i]=(1+rand()%9);
}
}
bool check_for_validity(int A[]){
int counter=0;
while (counter!=9){
numGenerator(A);
counter++;
}
for (int i=0; i<9; i++)
for (int j=0; j<9; j++){
if(A[j]==A[i])
numGenerator(A);
}
return true;
}
void main (){
//Descriptions and genral ideas
cout << "WELCOME TO SUDOKU " << endl;
cout << endl;
cout << "RULES: "<< endl;
cout << endl;
cout << "->You'll be given a 9x9 board with some numbers depending on which level of difficulty you choose to play."<< endl;
cout << endl;
cout << "->You have to arrange numbers from 1 to 9 so that a number shows up once in one row, one column and in a 3x3 box." << endl;
cout << endl;
cout << "So, let's get started" << endl;
cout << endl;
cout <<endl;
char dash[9][9];
for (int array=0; array<9; array++) {
for (int array2=0; array2<9; array2++) {
dash[array][array2]='_';
}
}
char row[9];
char column[9];
int num[81];
int num2[9][9];
int length;
length=strlen(row);
//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified.
for (int i=0; i<length; i++) {
dash[row[i]][column[i]]=num[i]+'0';
}
//Builds the Sudoko board and outputs the full 9x9 array.
cout << " 0 1 2 3 4 5 6 7 8 " << endl;
cout << "-----------------------------------------" << endl;
int i=0;
for (int count=0; count<3; count++) {
for (int count2=0; count2<3; count2++) {
cout << "||_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";
}
cout << "||" << i << endl;
i++;
}
cout << "-----------------------------------------" << endl;
int j=3;
for (int count=3; count<6; count++) {
for (int count2=0; count2<3; count2++) {
cout << "||_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";
}
cout << "||" << j << endl;
j++;
}
cout <<"-----------------------------------------" << endl;
int z=6;
for (int count=6; count<9; count++) {
for (int count2=0; count2<3; count2++) {
cout << "||_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";
}
cout << "||" << z << endl;
z++;
}
cout << "-----------------------------------------" << endl;
for (int row = 0; row < 9; row++) {
cout << "Enter values for row " << row + 1 << " : ";
for (int col = 0; col < 9; col++)
cin >> dash[row][col];
cout << endl << endl;
}
system("pause");
}
This whole section is wrong
char row[9];
char column[9];
int num[81];
int num2[9][9];
int length;
length=strlen(row);
//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified.
for (int i=0; i<length; i++) {
dash[row[i]][column[i]]=num[i]+'0';
}
You are using row and column even though they haven't got any values. Most likely this will crash your program.
Hard to know what you expected this to do. Perhaps you could explain?
Here's a suggestion for inputing values. Maybe you'll find it useful
// get the user's values
int row, column, value;
cout << "Enter a row number, column number, and value. All numbers should be between 1 and 9\n";
cin >> row >> column >> value;
// put the value in the board, add '0' to convert the integer to a digit
dash[row][column] = value + '0';