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.
Related
Most of the answers I'm coming across boil down to in infinite loop, but I've inserted some cout statements for diagnostic purposes and I'm thoroughly convinced this is not the case- it looks like the nested for loop finishes but simply skips over some lines before returning to the top of the outside for loop. The only time something gets pushed back is on the first iteration when the for nested loop is skipped completely. As is, the debugger gives an out of range vector fault, I assume because new unique items aren't being pushed back. Any ideas how to get around this?
Template <class T>
void count_unique(string filename, vector<T> input) {
vector<T> unique;
vector<int> count;
bool found = false;
ifstream fin;
fin.open(filename);
T line;
while (!fin.eof()) {
fin >> line;
input.push_back(line);
}
fin.close();
cout << input.size() << endl;
for (unsigned int i = 0; i < input.size(); i++) {
cout << input.at(i) << endl;
}
for (unsigned int i = 0; i < input.size(); i++) {
cout << input.at(i) << endl;
found = false;
for (unsigned int j = 0; j < unique.size(); i++) {
cout << unique.at(j) << "\t=\t" << input.at(i) << " ->" << unique.size() << endl;
if (input.at(i) == unique.at(j)) {
count.at(j)++;
found = true;
cout << "Incremented" << endl;
}
else {
cout << "None;" << endl;
}
}
cout << "Found=" << found << endl;
if (!found) {
unique.push_back(input.at(i));
count.push_back(1);
found = false;
cout << "Pushed back" << endl;
cout << "#";
for (unsigned int i = 0; i < unique.size(); i++) {
cout << unique.at(i) << "\t-\t" << count.at(i) << endl;
}
cout << "#" << endl;
}
}
for (unsigned int i = 0; i < unique.size(); i++) {
cout << "\t" << unique.at(i) << "\t=\t" << count.at(i) << endl;
}
}
Your inner loop incremements the wrong variable. It is actually an infinite loop, since j will never change. This is also why you get the out of range, i will continuously increase, beyond the size of input
//...
for (unsigned int i = 0; i < input.size(); i++) {
cout << input.at(i) << endl;
found = false;
for (unsigned int j = 0; j < unique.size(); j++) { // HERE
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'm printing random numbers enclosed in ascii boxes in a 6x6 grid. Having issues with printing out the grid.
Instead of having 6 columns, all my boxes and numbers are being printed out in 1 column. Been troubleshooting but cant seem to find the issue. Below is the code. Appreciate your assistance.
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy [6][6];
srand((unsigned)time(0));
int lowest=1111, highest=9999;
int range=(highest-lowest)+1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
cout << char(218);
for (int y=0; y< 4; y++)
{
cout << char(196);
}
cout << char(191) <<endl;
cout << char(179) << arrayxy[i][j] << char(179) << endl;
cout << char(192);
for (int z=0; z< 4; z++)
{
cout << char(196);
}
cout << char(217) <<endl;
}
cout << endl;
}
cout << endl;
}
You should endl after all the columns get printed out, NOT before.
This is how I fixed your code that gives, hopefully, what you wanted to have.
For the record, I specifically changed those ASCII to * and & to make the console result more readable. You can change them back to what you want those characters to be again.
void WriteFrontLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
void WriteEndLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy[6][6];
srand((unsigned)time(0));
int lowest = 1111, highest = 9999;
int range = (highest - lowest) + 1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest + int(range*rand() / (RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
WriteFrontLine(6);
for (int j = 0; j < 6; ++j)
{
cout << '*' << arrayxy[i][j] << '*';
}
cout << endl;
WriteEndLine(6);
cout << endl;
}
cout << endl;
}
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];
}
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';