How to display my Queue & Stack? - c++

I Wanna display my EvenQueue, EvenStack, OddQueue & EvenQueue? I already try some methods but the compiler gave me some errors.
Any help would be appreciated... Or any tips.
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main()
{
stack<int> OddStack;
queue<int> OddQueue;
stack<int> EvenStack;
queue<int> EvenQueue;
int MyNumbers[10];
int InNum;
for(int i = 0; i < 10; i++)
{
cout << "Enter Number " << i << ": ";
cin >> InNum;
MyNumbers[i] = InNum;
if(InNum % 2 == 0)
{
EvenQueue.push(InNum);
EvenStack.push(InNum);
}
else
{
OddQueue.push(InNum);
OddStack.push(InNum);
}
}
cout << "Stack" << "\t\t" << "Queue" << endl;
return 0;
}

Assign it to tempstack
stack<int>tempStack = OddStack
and start poping from it and see what is inside
while(tempStack.empty() == false){
int x = tempStack.top();
cout << x << endl;
tempStack.pop();
}
same goes for the queue

Related

How to start my else if for loop start with 01 - 09?

How do I make my code specifically on else if to print out this if my input is 45?
01.02.03.04.05.06.07.08.09.10
11.12.13.14.15.16.17.18.19.20
21.22.23.24.25.26.27.28.29.30
31.32.33.34.35.36.37.38.39.40
41.42.43.44.45
#include <iostream>
#include <string>
using namespace std;
int main() {
string dot = "";
int x;
cin >> x;
if (x<=10){
for (int n=1; n<=x; n++){
cout << dot << n ;
dot =".";
}
}
else if(x>10&&x<=100) {
for (int i = 1; i <=x; ++i){
for (int j = 1; j <=10; ++j){
cout << dot << x;
dot=".";
}
cout << endl;
}
}
else{
cout << "OUT OF RANGE";
}
return 0;
}
Your entire program can be simplified using setw and setfill to do the hard work for you of inserting leading zero chars where needed. #include <iomanip> to have access to these stream modification functions.
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
for (int i = 1; i <= x; i++)
{
cout << setw(2) << setfill('0') << i;
char delimiter = ((i % 10) && (i != x)) ? '.' : '\n';
cout << delimiter;
}
cout << endl;
}
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int x = 0;
cin >> x;
if (x > 100)
{
cout << "Out of range." << endl;
return 0;
}
for (int i = 1; i <= x; i++)
{
cout << setfill('0') << setw(2) << i << ((i % 10) ? "." : "\n");
}
return 0;
}

How to generate random numbers without duplicates using a loop in c++?

I am writing a random name generator without duplicates. I want to do it without other libaries like algorithm because I want to understand how to do that. Is there a way to do that with a loop or a vector?
I thaught of adding the names that have already been to a vector and then check all the elements of the vector with a while loop but I didn't know how I can do that.
#include <windows.h>
#include <conio.h>
#include <vector>
#include <ctime>
#include <iostream>
#include <ctime>
using namespace std;
int main() {
srand((unsigned)time(0));
int names = 1;
int a = 0;
int x = 0;
vector <string> vectornames;
cout << "How many names would you like to add: ";
int amount_of_names;
cin >> amount_of_names;
while (a < amount_of_names) {
cout << "Enter name " << names << ": ";
string name;
cin >> name;
vectornames.push_back(name);
names++;
a++;
}
while (x < amount_of_names) {
cout << "\n" << vectornames.at(x);
x++; }
cout << "\npress enter to continue: ";
getch();
system("cls");
int z = 0;
while (z < amount_of_names) {
int random_number = (rand() % amount_of_names);
if (vectornames.at(random_number) != vectornames.at(z)) {
cout << "Hello ";
cout << vectornames.at(z);
cout << "! You will get " << vectornames.at(random_number) << "\n";
vectornames.push_back(vectornames.at(random_number));
getch();
z++;
}}
cout << "\n\n";
return 0;
}
The last part has some problems, you select as many names are you have entered, don't know if that was intended lets just say it was. You add the selected names to the same vector, you don't check correctly for duplicates.
while (z < amount_of_names) { <-- same amount as was entered in original loop
int random_number = (rand() % amount_of_names);
if (vectornames.at(random_number) != vectornames.at(z)) { <--- doesn't check for duplicates
cout << "Hello ";
cout << vectornames.at(z);
cout << "! You will get " << vectornames.at(random_number) << "\n";
vectornames.push_back(vectornames.at(random_number)); <---- adding to same vector
getch();
z++;
}
}
Lets see what can be done without using algoritmes ... that means we need to implement std::find or use some other structure to keep check on what is already selected.
!!!Warning untested code!!!
bool Find(const std::string& needle, const std::vector<std::string>& haystack) const {
for(const auto& straw : haystack) {
if (straw == needle) {
return true;
}
}
return false;
}
And updated code, which use a different vector to store the used names.
std::vector<std::string> used;
while (z < amount_of_names) {
int random_number = (rand() % amount_of_names);
if (!Find(vectornames.at(random_number), used)) {
cout << "Hello ";
cout << vectornames.at(z);
cout << "! You will get " << vectornames.at(random_number) << "\n";
used.push_back(vectornames.at(random_number));
getch();
z++;
}
}

Adding conditions to a conditional statement

I am messing around with dynamic arrays for a user defined amount of inputs for an and gate.
The issue I am running into is that I don't know how many inputs the user is going to test and I need to be able to have an if-else statement that tests each input.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class logic_gate {
public:
int x = 0;
};
int main() {
int userInput = 0;
cout << "How many inputs do you want on your and gate?: ";
cin >> userInput;
cout << endl;
logic_gate *and_gate = new logic_gate[userInput];
cout << endl << "Please enter the values of each bit below . . ." << endl <<
endl;
int userTest1 = 0;
for (int i = 0; i < userInput; i++) {
cout << "#" << i + 1 << ": ";
cin >> userTest1;
and_gate[i].x = userTest1;
}
return 0;
}
Here is the code that I am currently trying to find a solution for.
To implement an AND gate with n inputs you can simply do:
int output = 1;
for (int i = 0; i < n; ++i)
{
if (!and_gate [i])
{
output = 0;
break;
}
}
// ...
Use Vector data structure, you don't need to tell its size while declaring, unlike array, and it can grow automatically.
To read input till it's arriving, put cin inside while loop condition. I used getline to read whole line and work with it, so that whenever user presses enter button at empty line, program will think that no more input is coming anymore, and will start calculating 'And' of inputs.
//don't forget to import vector
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class logic_gate {
public:
int x = 0;
logic_gate(){ //default constructor
}
logic_gate(int k){ //another constructor needed
x = k;
}
};
int main(){
cout << endl << "Please enter the values of each bit below . . ." << endl;
vector<logic_gate> and_gate; //no need to tell size while declaration
string b;
while(getline(cin, b)){ //read whole line from standard input
if (b == "\0") //input is NULL
break;
and_gate.push_back(logic_gate(stoi(b))); //to convert string to integer
}
if (!and_gate.empty()){
int output = and_gate[0].x;
for (int i = 1; i < and_gate.size(); i++){
output = output & and_gate[i].x;
}
cout << "And of inputs is: " << output << endl;
}
else{
cout << "No input was given!\n";
}
return 0;
}
Feel free to ask if some doubts linger
I figured out what I wanted to do. Thanks to everyone who helped and especially Paul Sanders. Below is my final code.
#include <iostream>
using namespace std;
class logic_gate {
public:
int x = 0;
};
int main() {
int userInput;
int output = 1;
cout << "How many inputs do you want on your and gate?: ";
cin >> userInput;
cout << endl;
logic_gate *and_gate = new logic_gate[userInput];
cout << endl << "Please enter the values of each bit below . . ." << endl <<
endl;
int userTest1;
for (int i = 0; i < userInput; i++) {
cout << "#" << i + 1 << ": ";
cin >> userTest1;
and_gate[i].x = userTest1;
}
if (userInput == 1) {
output = userTest1;
cout << "The test of " << userTest1 << " is " << output << endl << endl;
}
else if (userInput > 1) {
for (int i = 0; i < userInput; i++) {
if (!and_gate[i].x)
{
output = 0;
break;
}
}
cout << "The test of ";
for (int i = 0; i < userInput; i++) {
cout << and_gate[i].x;
}
cout << " is " << output << endl << endl;
}
return 0;
}

ACM: Web Navigation

Note:This problem I wrote is just for the people who know about ACM questions.
I have problem with this question. I wrote a good solution for this but every time I send it I get Wrong Answer. I don't know what's wrong here. I test this code for lots of test cases. Can you help me to fix my code?
Here is the link of the question: http://sharecode.ir/section/problemset/problem/1022.
Here is my code :
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
string page[1000] = { "" };
int cntr = 0;
page[cntr] = "http://www.acm.org/";
string page1;
while (1)
{
cin >> page1;
if (page1 == "QUIT")
break;
if (page1 == "VISIT")
{
cntr++;
cin >> page1;
page[cntr] = page1;
cout << page1 << endl;
}
if (page1 == "BACK")
{
cntr--;
if (cntr >= 0)
cout << page[cntr] << endl;
else
{
cout << "Ignored" << endl;
cntr = 0;
}
}
if (page1 == "FORWARD")
{
cntr++;
if (page[cntr] == "")
cout << "Ignored" << endl;
else
cout << page[cntr] << endl;
}
}
if (n) cout << endl;
}
}
You don't clear forwards after visiting a site. That's why you get wrong answer.
Here is the correct code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int n){
cin >> n;
while (n--){
string c;
vector <string> a(999,"\0");
a[0] = "http://www.acm.org/";
int i = 0;
while(cin>>c,c != "QUIT"){
if (c == "VISIT"){
i++;
string s;
cin >> s;
a[i] = s;
cout << a[i] << "\n";
int t = i+1;
while (a[t] != "\0") {
a[t] = "\0";
t++;
}
}
if (c == "BACK"){
i--;
if (i < 0) {cout << "Ignored\n"; i=0;} else cout << a[i] << "\n";
}
if (c == "FORWARD"){
i++;
if (a[i] == "\0") {cout << "Ignored\n"; i--;} else cout << a[i] << "\n";
}
}
if (n) cout << "\n";
}
}

C++ program doesn't print out strings but does output double value

So here is the program I'm working on. So far it just prints out the double value in my CPP file. Note the two characters arrays I have set up. Why is this?
Molecule.h
const int MAX_STRUCT = 10;
const int MAX_NAME = 20;
class Molecule {
char molecule_structure[];
char molecule_name[];
double molecule_mass;
public:
Molecule();
bool read();
void display() const;
};
Molecule.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "Molecule.h"
Molecule::Molecule() {
molecule_structure[0] = '\0';
molecule_name[0] = '\0';
molecule_mass = 0;
}
bool Molecule::read(){
bool complete = false;
cout << "Enter structure : ";
cin.getline (molecule_structure, 10);
if (strcmp (molecule_structure, "0") != 0){
cout << "Enter full name : ";
cin.getline (molecule_name, 20);
cout << "Enter weight : ";
cin >> molecule_mass;
cin.ignore();
complete = true;
}
else {
molecule_structure[0] = '\0';
molecule_name[0] = '\0';
molecule_mass = 0;
}
return complete;
}
void Molecule::display() const
{
cout << molecule_structure << " " << molecule_name << " " << molecule_mass << endl;
}
w4x.h
const int MAX_MOLECULES = 10;
w4x.cpp
#include <iostream>
using namespace std;
#include "w4x.h"
#include "Molecule.h"
int main() {
int n = MAX_MOLECULES;
Molecule molecule[MAX_MOLECULES];
cout << "Molecular Information\n";
cout << "=====================" << endl;
for (int i = 0; i < MAX_MOLECULES; i++) {
if (!molecule[i].read()) {
n = i;
i = MAX_MOLECULES;
}
cout << endl;
}
cout << "Structure Name Mass\n";
cout << "==================================================" << endl;
for (int i = 0; i < n; i++)
molecule[i].display();
}
The errors I believe are coming from my Molecule.cpp file which is what I've been changing around.
This is the output I'm currently receiving.
Molecular Information
=====================
Enter structure : Super
Enter full name : Man
Enter weight : 57
Enter structure : 0
Structure Name Mass
==================================================
57
Changing the header Molecule.h so it uses:
const int MAX_STRUCT = 10;
const int MAX_NAME = 20;
class Molecule {
char molecule_structure[MAX_STRUCT];
char molecule_name[MAX_NAME];
double molecule_mass;
public:
Molecule();
bool read();
void display() const;
};
Makes the code work sanely.
A more thorough reworking of the code to use std::string gives:
Molecule.h
#ifndef MOLECULE_H_INCLUDED
#define MOLECULE_H_INCLUDED
#include <string>
class Molecule
{
std::string molecule_structure;
std::string molecule_name;
double molecule_mass;
public:
Molecule();
bool read();
void display() const;
};
#endif // MOLECULE_H_INCLUDED
Molecule.cpp
#include <iostream>
#include <iomanip>
#include <limits>
#include <cstring>
using namespace std;
#include "Molecule.h"
Molecule::Molecule() : molecule_structure(""), molecule_name(""), molecule_mass(0) { }
bool Molecule::read()
{
Molecule m;
cout << "Enter structure : ";
if (!getline(cin, m.molecule_structure) || m.molecule_structure == "")
return false;
cout << "Enter full name : ";
if (!getline(cin, m.molecule_name))
return false;
cout << "Enter weight : ";
if (!(cin >> m.molecule_mass))
return false;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
swap(*this, m);
return true;
}
void Molecule::display() const
{
cout << left << setw(15) << molecule_structure << " ";
cout << left << setw(20) << molecule_name << " ";
cout << setprecision(5) << molecule_mass << endl;
}
The read() function does not modify the variable it is given unless the reading is successful. There are probably better ways to handle the input, but that shown is reasonably sensible. You terminate the input with an empty line in response to the 'Enter structure:' prompt. The printf() format notations have the merit of brevity compared with what's necessary with C++ I/O streams.
w4x.cpp
No longer including w4x.h.
#include <iostream>
using namespace std;
#include "Molecule.h"
const int MAX_MOLECULES = 10;
int main()
{
int n = MAX_MOLECULES;
Molecule molecule[MAX_MOLECULES];
cout << "Molecular Information\n";
cout << "=====================" << endl;
for (int i = 0; i < MAX_MOLECULES; i++) {
if (!molecule[i].read()) {
n = i;
break;
}
cout << endl;
}
if (n > 0)
{
cout << "Structure Name Mass\n";
cout << "===================================================" << endl;
for (int i = 0; i < n; i++)
molecule[i].display();
}
}