Why is my grapher printing multiple instances of the function? C++ - c++

I have this program that graphs simple parametric equations on a board of a defined length and width by me. It compiles fine but prints multiple instances of the function in different positions of the graph. If someone could please help me figure out why I am getting this output, I would greatly appreciate it. I included comments throughout the code to help understand what is going on.
I do not have enough reputation to post a picture of the output but if you compile and execute it you will see what I am talking about.
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
using namespace std;
#define N 25
#define M 60
/*
This function prints the board each time it is called and places an *
in the place corresponding to the value of the function.
*/
void print_board(char p[M][N]) {
int i, j;
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++)
if (i==0) cout << '=';
else if (j==0) cout << '|';
else if (i==N) cout << '=';
else if (j==M) cout << '|';
else if (p[i][j]== '*') cout << '*';
else cout << ' ';
cout << endl;
}
}
/*
These functions accepts an integer for time and computes a value for x and y
for the parametirc equations given and returns each.
*/
int fx(int t) {
int x = t;
return x;
}
int fy(int t) {
//int y = 5 * sin(0.2 * t) + 15;
int y = (pow(t,2)/60) - t + 25;
return y;
}
/*
This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[M][N], int t) {
int i, j;
//copies the old board
int q[M][N];
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++) {
q[i][j] = p[i][j];
}
}
//creates the new board
int x, y;
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++) {
x = fx(t);
y = fy(t);
if (i == y && j == x) {
p[i][j] = '*'; //stores an * for the values of x and y
}
}
}
}
int main() {
char p[M][N];
print_board(p);
int t = 0;
while(t <= M) {
cout << string(80, '\n');
next_board(p , t);
print_board(p);
usleep(20000);
t++;
}
return 0;
}
Please help and thank you for all who try!

everywhere in your program where you have
char p[M][N]
change them to
char p[N][M]
and you should get the results that youd expect, your mixing axes in your program
heres the whole working code if youd like
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
#include <string>
using namespace std;
#define N 25
#define M 60
/*
This function prints the board each time it is called and places an *
in the place corresponding to the value of the function.
*/
void print_board(char p[N][M]) {
int i, j;
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++)
if (i == 0) cout << '=';
else if (j == 0) cout << '|';
else if (i == N) cout << '=';
else if (j == M) cout << '|';
else if (p[i][j] == '*') cout << '*';
else cout << ' ';
cout << endl;
}
}
/*
These functions accepts an integer for time and computes a value for x and y
for the parametirc equations given and returns each.
*/
int fx(int t) {
int x = t;
return x;
}
int fy(int t) {
//int y = 5 * sin(0.2 * t) + 15;
int y = (pow(t, 2) / 60) - t + 25;
return y;
}
/*
This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[N][M], int t) {
int i, j;
//copies the old board
int q[M][N];
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++) {
q[i][j] = p[i][j];
}
}
//creates the new board
int x, y;
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++) {
x = fx(t);
y = fy(t);
if (i == y && j == x) {
p[i][j] = '*'; //stores an * for the values of x and y
}
}
}
}
int main() {
char p[N][M];
print_board(p);
int t = 0;
while (t <= M) {
cout << string(80, '\n');
next_board(p, t);
print_board(p);
usleep(20000);
t++;
}
return 0;
}

Related

asterisk rectangle using while loop

I have to make an i by j rectangle using while loops....
so far this is as far I got.
#include <iostream>
using namespace std;
void stars(int, int);
int main()
{
int i, j;
cin >> i >> j;
stars(i, j);
return 0;
}
void stars(int i, int j)
{
while (j >= 0)
{
while (i >= 1)
{
cout << "*";
i = i - 1;
}
j = j - 1;
}
}
it shoots out one row of 'i' asterisks.
I (j-1) more rows....
There are 2 things wrong with your stars code:
You need to somehow go to the new line after your row of asterisks is complete
If you decrement i and never restore it to its original value after the 1st row no more asterisks will be printed
You could try something like this:
void stars(int i, int j)
{
while (j-- > 0)
{
int k = i;
while (k-- > 0)
cout << "*";
cout << endl;
}
}

LRU c++ program

I've been working on a program in one of my college classes. I have been having trouble with the implementation of my LRU code as it is not displaying any errors or anything, but compiles. There are two parts. The main that we input the values into, which we then specify which algorithm we want to use to find page faults. I know the main works, along with the FIFO algorithm, but I'm not getting anything with my LRU code (It compiles and "runs" but displays nothing as if I did not click to use the algorithm). Can anyone help me figure out what is wrong?
main.cpp
#include <iostream>
#include <string>
//#include "fifo.cpp"
#include "lru.cpp"
//#include "optimal.cpp"
using namespace std;
int main() {
// List of different variables
string pagestring;
int fs,pn[50], n;
// Prompt for page references
cout<<"Virtual Memory Simulation\nBy blah\n----------\nEnter the number of pages : " << endl;
cin >> n;
cout<<"\n-------------\nPlease enter a list of page numbers separated by commas.\n"<< endl;
cin>>pagestring;
// algorithm to use
char algo;
while (true) {
// Prompt algorithm to use
cout<<"----------\nPlease select an algorithm to use.\n\n1: First-In-First-Out (FIFO)\n2: Least-Recently-Used (LRU)\n3: Optimal\n0: Quit\n"<<endl;
cin>>algo;
if (algo == '1') {
//fifo(pagestring);
}
else if (algo == '2'){
LRU_Execute(pagestring, n);
}
else if (algo == '3'){
cout<<"Optimal Not yet coded"<<endl;
}
else if (algo == '0'){
break;
}
else {
cout<<"Invalid choice. Please try again."<<endl;
}
}
cout<<"Goodbye!!"<<endl;
};
LRU.cpp
#include <iostream>
#include <string>
using namespace std;
class pra
{
int fs,z;
int frame[50], frame1[50][2], pn[50], n, cnt, p, x;
public:
pra();
void init(string pagestring);
void getdata(string pagestring, int n);
void lru(int* pn, int n, string pagestring);
};
pra::pra()
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::init(string pagestring)
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::getdata(string pagestring, int n)
{
fs=3;
// index to loop through input string
int i = 0;
// current input string character
char z = pagestring[i];
int x = 0;
//cout << "\nEnter the page numbers : ";
while (z != '\0'){
// skip over commas and spaces
if (!(z == ',')) {
pn[x] = z;
x++;
// cout<<pn[x]<<"-This is pn[x]\n";
}
z = pagestring[++i];
}
//cout<<pn[x]<<"-This is pn[x] AGAIN\n";
this->lru(pn, n, pagestring);
}
void pra::lru(int* pn, int n, string pagestring)
{
init(pagestring);
int ind = 0, fault = 0, pi = 0, j, fn;
char i, z;
p = 0;
cnt = 0;
int min;
cout<<n<<"---"<<i<<" - "<<j<<" - "<<" - "<<fn<<" - "<<z;
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
pi = 0;
for (i = 0; i < n; i++)
{
j = 0;
if (ind > fs - 1)
ind = 0;
fault = 1;
min = 999;
while (j < fs)
{
if (frame1[j][0] = pn[pi])
{
fault = 0;
p++;
frame1[j][1] = p;
goto l2;
}
if (frame1[j][1] < min)
{
min = frame1[j][1];
fn = j;
}
j++;
}
j = 0;
while (j < fs)
{
if (frame1[j][0] = -1)
{
fault = 1;
fn = j;
goto l2;
}
j++;
}
ind++;
l2:
if (fault == 1)
{
p++;
frame1[fn][0] = pn[pi];
frame1[fn][1] = p;
cnt++;
}
cout << "\nElement: " << pn[pi];
pi++;
for (z = 0; z < fs; z++)
{
cout << "\t" << frame1[z][0];
}
if (fault == 1)
cout << "\t**Page Fault**";
else
cout << "\t--No Page Fault--";
}
cout << "\nTotal number of page faults: " << cnt;
cout << "\n";
}
void LRU_Execute(string pagestring, int n)
{
pra p;
int j, fault = 0, i, pi, z, fn, ind = 0, ans, ch;
p.getdata(pagestring, n);
//p.lru();
while (ans == 1);
//return 1;
}

why is vector subscript out of range

I have a vector full of monster objects which are initialized onto a 10X10 map which works. I am now playing with some code to prevent monsters being spawned on the same map co-ordinate. when i run the code it cuts and brings up "vector subscript out of range" and i have no idea why. Any help would be great.
main function
#include "stdafx.h"
#include "character.h"
#include "monster.h"
#include "player.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
vector<monster*> monVec;
vector<int> monx;
vector<int> mony;
player player1;
bool collision();
void initialise();
int main(){
initialise();
player1.moveChar(3, 6);
bool temp;
temp = collision();
system("pause");
return 0;
}
initialize function
void initialize()
{
srand(time(NULL));
for (int n = 0; n < 10; n++)
{
int inx = rand() % 9;
int iny = rand() % 9;
if (n == 0){
monx.push_back(inx);
mony.push_back(iny);
}
for (int i = 0; i < n; i++)
{
-------->if (inx != monx[i] && iny != mony[i]){
monx.push_back(inx);
mony.push_back(iny);
}
else n--;
}
monVec.push_back(new monster());
monVec[n]->moveChar(inx, iny);
cout << endl << inx << ", " << iny << endl;
}
}
cout is just to check if its working once it runs and arrow indicates problem line.
thanks
In your initialize
you do the following
<for 10 times>
<when first time, add one item to the x,y vectors>
<access the up to 10nth element of the x,y vectors> //But vectors are only guaranteed to have at least one element each
<maybe add one item to the x,y vectors>
Problem is already that there is a path where there are not enough elements in your vectors. Plus the mistake about assignment and comparison in your if like #Michael Waltz already mentioned.
void initialize()
{
srand(time(NULL));
for (int n = 0; n < 10; n++)
{
int inx = rand() % 9;
int iny = rand() % 9;
if (n = 0){ //<<--------------------------- replace (n = 0) by (n == 0)
monx.push_back(inx);
mony.push_back(iny);
}
for (int i = 0; i < 10; i++)
{
// <<<< here monx and mony may contain only
// one element so monx[1] and mony[1] are invalid
if (inx != monx[i] && iny != mony[i]){
monx.push_back(inx);
mony.push_back(iny);
}
else n--;
}
monVec.push_back(new monster());
monVec[n]->moveChar(inx, iny);
cout << endl << inx << ", " << iny << endl;
}
}

c++ output of stars using recursion

I am trying to do this exercise.
Write a recursive function to generate a pattern of stars such as the following:
*
**
***
****
****
***
**
*
It seems simple but is giving me a lot of problems. I can only output this
*
**
***
****
using the following code
#include <iostream>
using namespace std;
void outputStars(int);
int main()
{
outputStars(5);
return 0;
}
void outputStars(int num)
{
if (num == 1)
{
return;
}
outputStars(--num);
for (int i = 0; i < num ; i++)
{
cout << "*";
}
cout << endl;
}
You are only printing the stars when you return from the call. Also print it before you call.
I've modified it to take 2 arguments. i specifies number of times * is to be printed in a call
void outputStars(int num,int i)
{
if (i == num)
{
return;
}
for (int j = 0 ;j < i; j++)
{
cout << "*";
}
cout << endl;
outputStars(num,i+1);
for (int j = 0; j < i; j++)
{
cout << "*";
}
cout << endl;
Call it as
outputStars(5,1);
#include <stdio.h>
void rec_fun(int x);
void print_stars(int times, int x);
int main(int argc, char *argv[])
{
rec_fun(5);
return 0;
}
void rec_fun(int x)
{
static int times = x;
if (x <= 0)
return;
print_stars(times, x);
rec_fun(x - 1);
if (x != 1)
print_stars(times, x);
}
void print_stars(int times, int x)
{
int i;
for (i = 0; i < times - x + 1; i++)
printf("*");
printf("\n");
}

Trying to figure out an issue with corrupted stack while adding int arrays in c++

I am working on visual studio 2013, with a windows8 hp.My code is trying to add two int arrays of size [20] and output the sum. I know I am out or range some where ,but I can't seem to find where. I am the first digit from each array during my convert function and my answer[i] output is only 19 digits when it should be 21digits.
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;
int globalnum[20];
int total[21];
int i;
void convert(char[], int);
void add(int[], int[], int);
void printAnswer(int[], int);
int main()
{
char n1[20];
char n2[20];
int num1[20];
int num2[20];
int answer[21];
cin >> n1 >> n2;
int l1 = strlen(n1);
int l2 = strlen(n2);
int max = fmax(l1, l2);
convert(n1, l1);
for (int i = 0; i < max - 1; i++)
num1[i] = globalnum[i];
for (int i = 0; i < max; i++)
cout << num1[i];
cout << endl;
convert(n2, l2);
for (int i = 0; i < max - 1; i++)
num2[i] = globalnum[i];
for (int i = 0; i < max; i++)
cout << num2[i];
cout << endl;
add(num1, num2, max);
for (int i = 0; i < max - 1; i++)
answer[i] = total[i];
// printAnswer(answer,max);
for (int i = 0; i < max - 1; i++)
cout << answer[i];
return 0;
}
void convert(char c1[], int size)
{
for (int i = 0; i < size - 1; i++)
globalnum[i] = c1[size - 1 - i] - '0';
}
void add(int add1[], int add2[], int s1)
{
int sum[21];
int remain = 0;
for (i = 0; i < s1 - 1; i++)// This starts to add the numbers.
{
sum[i] = (add1[s1 - 1 - i] + add2[s1 - 1 - i] + remain) % 10;
if (add1[s1 - 1 - i] + add2[s1 - 1 - i] + remain >= 10)
remain = 1;
else
remain = 0;
if (remain != 0)
total[s1 - 1 - i] = 1;
else total[s1 - 1 - i] = 0;
total[s1 - 1 - i] = sum[i];
}
if (remain != 0)
total[0] = 1;
}
//void printAnswer(int t[], int b)
// {
// for (int i = b - 1; i < 0; i--)
// cout << t[i];
// }
// cout << endl;
//}
There's too many problems with your code to give a simple answer. It's maybe easier to work backwards from working code. You haven't fully specified the problem but this was my best guess at what you're trying to do:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
vector<int> toInts(const string& s) {
vector<int> v(s.size());
transform(cbegin(s), cend(s), begin(v), [](int c) { return c - '0'; });
return v;
}
int main() {
string a, b;
cin >> a >> b;
auto n = toInts(a);
auto m = toInts(b);
const auto size = max(n.size(), m.size());
n.resize(size);
m.resize(size);
vector<int> sums(size);
transform(cbegin(n), cend(n), cbegin(m), begin(sums), plus<>{});
copy(cbegin(sums), cend(sums), ostream_iterator<int>{cout, ", "});
cout << endl;
}