Abnormal Output from program - c++

I have a program that should print out 2 lists like the one bellow, basically the same list but backwards, however it works the first time but then it prints some weird output which I Will show bellow as well.
0123456789
9876543210
however the actual output I get from my program is this:
Can anyone tell me what is wrong with my code please, I am not sure why I am getting this output.
void createFile(){
ofstream myfile;
myfile.open ("TEST1.txt");
for(int x = 0; x < 10; x++){
myfile << x << "\n";
}
myfile.close();
}
void popArray(int array1[]){
ifstream infile("TEST1.txt");
int x;
while(infile >> array1[x]){
cout << array1[x];
}
}
void reverseList(int array2[]){
for(int x = 9; x > -1; x--){
cout << setw(2) << array2[x];
}
}
void checkLists(int array1[], int array2[], int sizeOfArray){
for(int x = array1[1]; x < sizeOfArray; x++){
for(int y = array2[1]; x < sizeOfArray; x++){
if(x == y)
cout << "Palindrome" << endl;
else{
cout << "Not" << endl;
}
}
}
}
int main()
{
int array1[10];
int array2[10];
createFile();
popArray(array1);
cout << "\n";
reverseList(array1);
cout << "\n";
checkLists(array1, array2, 10);
}

This is the problem I think:
void popArray(int array1[]){
ifstream infile("TEST1.txt");
int x;
while(infile >> array1[x]){
cout << array1[x];
}
}
You never specify what x is. If I understand what you're trying to do I think this should work:
void popArray(int array1[]){
ifstream infile("TEST1.txt");
int x;
for (x=0; x < 10; x++){
cout << array1[x];
}
}
Just read through a bit more. You've also got errors here:
void checkLists(int array1[], int array2[], int sizeOfArray){
for(int x = array1[1]; x < sizeOfArray; x++){
for(int y = array2[1]; x < sizeOfArray; x++){
if(x == y)
cout << "Palindrome" << endl;
else{
cout << "Not" << endl;
}
}
}
}
I'd do something like:
bool checkLists(int array1[], int array2[], int sizeOfArray){
bool isPalindrome=true;
for(int x = 0; x < sizeOfArray; x++){
if(array1[x] != array2[sizeOfArray-(x+1)]){
isPalindrome = false;
}
}
return isPalindrome;
}
Then at the end of main you could have:
if(checkLists(array1, array2, 10)){
cout << "Is Palindrome\n";
}
else{
cout << "Is Not Palindrome\n";
}
While I'm at it I might as well fix this too:
void reverseList(int array2[]){
for(int x = 9; x > -1; x--){
cout << setw(2) << array2[x];
}
}
Change this to:
void reverseList(int array1[], int array2[]){
for(int i = 0; i < 10; i++){
array2[9-i] = array1[i];
}
}
I think if you put together all the bits of my answer you should more or less have something that works. I've not tested it myself though.

void popArray(int array1[]){
ifstream infile("TEST1.txt");
int x;
while(infile >> array1[x]){
cout << array1[x];
}
}
You are not changing x in your loop (or initializing x at all!)

for(int x = array1[1]; x < sizeOfArray; x++){
for(int y = array2[1]; x < sizeOfArray; x++){if(x == y)
cout << "Palindrome" << endl;
else{
cout << "Not" << endl;
}
}
i this you should replace you code by this one
for(int x = 0; x < sizeOfArray; x++){
for(int y = 0; y < sizeOfArray; y++){
if(array1[x]== array2[y])
cout << "Palindrome" << endl;
else{
cout << "Not" << endl;
}
}

Related

Problem with checking deck for held card. C++

I'm very new and attempting to create a deck that will be filled at random from cardpool, permterm will also be filled with the same cards. This will give you duplicates of some cards and singles of others, as intended. Then you can use a card or draw a card and it will sort hand() and deckArr() correctly.
The problem comes when you draw and use a card back to back. When a card is in your hand, the corresponding trigger in cardActivation will turn to 1. (Meaning it's in your hand and cannot be drawn again) So if you draw, it handles the activation for that one card and shifts the deck accordingly. If you use the card, it is the opposite. It will turn the trigger to 0 (Meaning its available to draw and inside the deck) and shift the hand accordingly.
It seems that it randomly decides not to run the if statement within draw() [if (cardActivation[x] == 0 && hand[handSize-1] == permterm[x])] and will not trigger the activation, thus throwing off the rest of the program, but the next iteration of the loop correctly runs the if statement. When I get to the end of all the testing the activation is off by 1 because of the one time it missed the if statement. (There should be nine 1s instead of eight.)
I've fiddled and changed my code 100000 times but can't figure out why randomly it decided to skip the if statement, as far as I can tell it should still be true.
I've left my notes and tests within my code so hopefully it can help others understand my thinking process, sorry it's such a mess. It was MUCH cleaner before I started bug fixing. The HP bar information can be ignored, it's apart of something else. Hopefully I explained well enough for some help, I'm losing my mind. Thank you in advance!
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class healthBar
{
private:
int hp;
public:
healthBar()
{
hp = 100;
}
int damage(int dmg)
{
hp-=dmg;
if (hp == 0)
{
cout << "Youve Died " << endl;
resetHP();
return 0;
}
else
{
printHP();
return 1;
}
}
void heal(int regen)
{
hp+=regen;
}
void printHP()
{
cout << "HP is: " << hp << endl;
}
void resetHP()
{
hp = 100;
}
};
class myDeck
{
private:
int deckArr[30];
int tempDeck[30];
int permterm[30];
int hand[10] = {0,0,0,0,0,0,0,0,0,0};
int handSize = 0;
int cardPool[25] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10
, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
int cardActivation[30];
public:
myDeck()
{
//Attaches Deck
int x;
srand(time(0));
for (int y = 0; y < 30; y++)
{
x = rand()%25;
deckArr[y] = cardPool[x];
cout << deckArr[y] << endl;
}
cout << "Permterm:" << endl;
for (int z = 0; z < 30; z++)
{
permterm[z] = deckArr[z];
cardActivation[z] = 0;
cout << permterm[z] << endl;
}
/*If you wanted duplicates of cards but
also be able to reject if that specific card has
been drawn already, you'd need an
activation array lined up with the permterm. 0=Not Active (Dont Skip) 1= In Hand (Skip)
It will need to search for the number.*/
}
void shuffle()
{
int z = 0;
for (int x = 0; x<30; x++)
{
tempDeck[x] = deckArr[x];
}
srand(time(0));
int y;
for (int x = 0; x<30; x++)
{
y = rand()%30;
if (y != z)
{
deckArr[x] = tempDeck[y];
}
z = y;
}
cout << "\nShuffled!" << endl;
}
void drawHand()
{
handSize=7;
for (int x = 0; x < 7; x++)
{
hand[x] = deckArr[x];
}
for (int x = 0; x<30; x++)
{
deckArr[x] = deckArr[x+7];
}
for (int x = 23; x < 30; x++)
{
deckArr[x] = 0;
}
//Switch On and Search
for (int y = 0; y < handSize; y++)
{
for (int x = 0; x < 30; x++)
{
if (hand[y] == permterm[x] && cardActivation[x] == 0)
{
cardActivation[x] = 1;
break;
}
}
}
cout << "\nHand Drawn!" << endl;
}
void viewDeck()
{
for (int x = 0; x<30; x++)
{
cout << permterm[x] << " " << cardActivation[x] << endl;
}
}
void viewHand()
{
for (int x = 0; x<handSize; x++)
{
cout << hand[x] << endl;
}
}
void resetDeck()
{
int resetCard;
int handTemp = handSize;
for (int y = 0; y < 30-handTemp; y++)
{
for (int x = 0; x<30;x++)
{
if (cardActivation[x] != 1)
{
deckArr[y] = permterm[x];
break;
}
}
}
cout << "Reset!"<< endl;
}
void draw()
{
if (handSize < 10)
{
cout << "\nDrawing..." << endl;
hand[handSize] = deckArr[0];
cout << "You drew: " << deckArr[0] << endl;
handSize++;
for (int x = 0; x < 30; x++)
{
deckArr[x] = deckArr[x+1];
}
deckArr[29] = 0;
//Activation
for (int x = 0; x < 30; x++)
{
if (cardActivation[x] == 0 && hand[handSize-1] == permterm[x])
{
cardActivation[x] = 1;
viewDeck();
break;
}
}
}
else
{
cout << "\nYour hand is full!" << endl;
}
//Test to see if deck needs to be reset
int sum = 0;
for (int x = 0; x < 30; x++)
{
sum+=deckArr[x];
}
//Checks to see if sending to resetDeck.
if (sum == 0)
{
viewDeck();
resetDeck();
}
}
void useCard()
{
int input;
cout << "\nWhich card would you like to use?" << endl;
for (int y = 0; y < handSize; y++)
{
cout << y+1 << ". " << hand[y] << endl;
}
cin >> input;
cout << "\nYou used: " << hand[input-1] << endl;
//Deactivate
for (int x = 0; x < 30; x++)
{
if (cardActivation[x] == 1 && hand[input-1] == permterm[x])
{
cardActivation[x] = 0;
break;
}
}
viewDeck();
for (int y = input-1; y<handSize; y++)
{
if (y+1 == 10)
{
hand[y] = 0;
}
else
hand[y] = hand[y+1];
}
handSize--;
}
};
int main()
{
/*Objective: Create a game where you draw from a deck of cards, to have 7 cards in hand.*/
healthBar health;
myDeck deck;
deck.viewDeck();
cout << "\nShuffling..." << endl;
deck.shuffle();
deck.viewDeck();
cout << "\nDrawing Hand..." << endl;
deck.drawHand();
deck.viewHand();
cout << "\nIs the Deck missing 7 cards?" << endl;
deck.viewDeck();
cout << "Can you draw? Hand:" << endl;
deck.draw();
deck.viewHand();
cout << "\nIs the deck missing 8 cards?" << endl; deck.viewDeck();
cout << "\nCan you draw until hand is full?" << endl;
deck.draw();
deck.viewHand();
deck.draw();
deck.viewHand();
deck.draw();
deck.viewHand();
cout << "\nCan you use a card?" << endl;
deck.useCard();
deck.draw();
deck.useCard();
deck.draw();
deck.useCard();
deck.draw();
deck.useCard();
for (int x = 0; x <10; x++)
{
deck.draw();
deck.useCard();
}
cout << "\nDoes resetting exclude your hand?" << endl;
cout << "Hand: " << endl;
deck.viewHand();
cout << "Deck: " << endl;
deck.viewDeck();```

C++ function will not print array to console in reverse

I'm working on homework for my c++ class we have just started arrays. I cannot figure out why the function reverse_array() does not print to the console when the function show_array() prints to the console without an issue. I've attempted googling the issue without success. I am just beginning c++ so it could be something small that I'm overlooking,. I appreciate any help.
#include<iostream>
using namespace std;
double dbval[5];
void fill_array(int x, double dbval[]);
void show_array(int x, double dbval[]);
void reverse_array(int x, double dbval[]);
int main(){
int x = 0;
fill_array(x,dbval);
show_array(x,dbval);
reverse_array(x,dbval);
return 0;
}
void fill_array(int x, double dbval[]){
int count = 0;
for (x = 0; x < 5; x++){
cin >> dbval[x];
if(!cin){
break;
}
}
for(x = 0; x < 5; x++){
count = count + 1;
}
cout << "Entries " <<int(count);
cout <<endl;
}
void show_array(int x, double dbval[]){
for (x = 0; x<5;x++){
cout << dbval[x] << " ";
}
cout << endl;
}
void reverse_array(int x, double dbval[]){
for(x = 5; x < 5; x--){
cout << dbval[x] << " ";
}
cout << endl;
}
This loop for(x = 5; x < 5 ; x--) failed because it did not satisfy your condition the first time it checked (x = 5, but it needs to be smaller than 5 to enter the loop)
Change to this:
void reverse_array(int x, double dbval[]){
for(x = 4; x >= 0 ; x--){
cout << dbval[x] << " ";
}
cout << endl;
}

How to return two values ​in an array and sum them separately

I would like to know how to sum t1[] and t2[] separately. What i need to do in this exercise is to write a program that takes two 5-digit series of integers from the user and stores them in two arrays. Then the program should calculate the sum of the elements in each array and tell you which of the arrays is larger. I'm not sure if I'm doing this correctly ;/
#include <iostream>
using namespace std;
void pobierzliczby1(int t1[]) {
int liczba1 = 0;
for (int i = 0; i<5; i++){
cout << "Podaj liczbe: ";
cin >> liczba1;
t1[i] = liczba1;
}
}
void pobierzliczby2(int t2[]) {
int liczba2 = 0;
for (int i = 0; i<5; i++){
cout << "Podaj liczbe: ";
cin >> liczba2;
t2[i] = liczba2;
}
}
float suma(int t1[], int t2[]){
float suma1 = 0;
float suma2 = 0;
for (int i = 0; i<5; i++){
suma1+= t1[i];
suma2+=t2[i];
}
return tuple(suma1, suma2);
}
int main(int argc, char **argv)
{
int i;
int t1[i];
int t2[i];
int suma1;
int suma2;
pobierzliczby1(t1);
cout << "Teraz seria druga!" << endl;
pobierzliczby2(t2);
cout << "Suma 1 wynosi: " << suma1 << endl;
cout << "Suma 2 wynosi: " << suma2 << endl;
}
What you are missing is that you can write one function and use it on each array separately. Like this
void pobierzliczby(int t[]) {
int liczba1 = 0;
for (int i = 0; i<5; i++){
cout << "Podaj liczbe: ";
cin >> liczba1;
t[i] = liczba1;
}
}
float suma(int t[]){
float suma = 0;
for (int i = 0; i<5; i++){
suma+= t[i];
}
return suma;
}
int main(int argc, char **argv)
{
int t1[5];
int t2[5];
int suma1;
int suma2;
pobierzliczby(t1);
cout << "Teraz seria druga!" << endl;
pobierzliczby(t2);
suma1 = suma(t1);
suma2 = suma(t2);
cout << "Suma 1 wynosi: " << suma1 << endl;
cout << "Suma 2 wynosi: " << suma2 << endl;
}
You don't have to write different functions that do the same thing just because the functions are working with different variables. The function can be called with different variables and the parameters in the function get the values of whatever variables were used to call the function.
Some other mistakes in your code. You wrote
int t1[i];
int t2[i];
but your arrays are suppposed to be size 5, so it should be
int t1[5];
int t2[5];
The variable i in main is unused, so it can be deleted.
Your suma function is declared as returning a float, but all you other variables and arrays are int. Probably suma should be int as well.
You can do something like this without using functions
int main(){
int t1[5], t2[5];
int sum1 = 0, sum2 = 0;
for(int i = 0; i < 5; i++){
cin >> t1[i];
}
for(int i = 0; i < 5; i++){
cin >> t2[i];
}
for(int i = 0; i < 5; i++){
sum1 += t1[i];
sum2 += t2[i];
}
cout << "First sum: " << sum1;
cout << "Second sum: " << sum2;
}

C++ Compare elements in an array and print the position

I want to compare elements for each line of a matrix, from smallest to biggest.
But I don't want to print the sorted array I want to print the original position.
0 11.80 79.34 78.23
11.80 0 65.23 45.19
79.34 65.23 0 90.27
78.23 45.19 90.27 0
In this Matrix for the first line I wanna print 1, 2, 4, 3
My Code so far:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
string dummy;
double myArray[4][4];
int i;
int j;
int y;
ifstream infile("dist.dat");
cout << "Open file " << "dist.dat" <<" for reading." << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
infile >> myArray[i][j];
if (!infile) {
cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl;
return 0;
}
}
}
infile.close();
cout << "Here's the array from the file" << endl;
cout << fixed << setprecision(2);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
cout << setw(10) << myArray[i][j];
}
cout << endl;
}
cout << endl;
int x = myArray[i][j];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if(myArray[i][j] >= x) {
x = j;
}
else {
x = j + 1;
}
}
cout << x << endl;
}
return 0;
}
You need to maintain another matrix which has indices of every line and then apply the same operations on it that you are applying on each line of the original matrix to sort it.
Here is the code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
void swap(double &x, double &y)
{
double temp = x;
x = y;
y = temp;
}
int main()
{
string dummy;
double myArray[4][4];
int i;
int j;
int y;
int k;
ifstream infile("dist.dat");
cout << "Open file " << "dist.dat" <<" for reading." << endl;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
infile >> myArray[i][j];
if (!infile)
{
cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl;
return 0;
}
}
}
infile.close();
cout << "Here's the array from the file" << endl;
cout << fixed << setprecision(2);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << setw(10) << myArray[i][j];
}
cout << endl;
}
cout << endl;
int sortedIndices[4][4];
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
sortedIndices[i][j] = j+1;
}
}
int x;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
x = j;
for(k = j+1; k < 4; k++)
{
if(myArray[i][k] < myArray[i][x])
{
x = k;
}
}
swap(sortedIndices[i][j], sortedIndices[i][x]);
swap(myArray[i][j], myArray[i][x]);
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << setw(10) << sortedIndices[i][j];
}
cout<<endl;
}
return 0;
}

How do i draw a hollow diamond in C++ using two fuctions

So me and my group have been working on this project for a few days we can get the top part too draw when we step into it but it does not run and we are not sure why it wont.
The first function is to draw the top and the second is to draw the bottom.
The top seems to draw fine i am not sure why the code will not even run.
#include<iostream>
using namespace std;
void drawTopPart(int userNum);
void drawBottomPart(int userNum);
int main() {
//declarations
int userNum;
//get user input
cout << "Enter an odd number from 1 to 15: ";
cin >> userNum;
//output
drawTopPart(userNum);
drawBottomPart(userNum);
cout << endl;
system("pause");
return 0;
}
void drawTopPart(int userNumPar) {
int z = 1;
int size;
cin >> size;
for (int i = 0; i <= size; i++) {
for (int j = size; j>i; j--) {
cout << " ";
}
cout << "*";
if (i>0) {
for (int k = 1; k <= z; k++) {
cout << " ";
} z += 2; cout << "*";
}
cout << endl;
}
}
void drawBottomPart(int userNumPar){
int z -= 4;
int size;
cin >> size;
for (int i = 0; i <= size - 1; i++) {
for (int j = 0; j <= i; j++) {
cout << " ";
}
cout << "*";
for (int k = 1; k <= z; k++) {
cout << " ";
}
z -= 2;
if (i != size - 1) {
cout << "*";
}
}
You have two mainly errors.
First,
void drawBottomPart(int userNumPar){
int z -= 4;
int size;
int z -= 4; have syntax error.You can change it int z = 17;
Second,you forget endl here.
if (i != size - 1) {
cout << "*";
}
cout << endl;