I'm currently self teaching C++, and have a problem. the purpose of this code is to print asterisks in a pyramiding fashion, with the example being if the input (variable int n) is 5, it should print like this:
*
**
***
****
*****
Here's the code:
#include <iostream>
using namespace std;
int main()
{
int i, n;
cout << "What is your number?" << endl;
cin >> n;
cout << "n is: " << n << endl;
int arr[n];
int z = n;
while(z > 0){
arr[z] = 0;
z--;
}
z = n;
for(int y = 0; y<=z; y++){
arr[y] = z;
cout << "Y is: " << y << endl;
cout << "Arr[Y] is: " << arr[y] << endl;
cout << "z is: " << z << endl;
z--;
}
while(n > 0){
int x = arr[n];
while(x > 0){
cout << "*";
x--;
}
cout << endl;
n--;
}
}
but the first half (rounded up) will always be blank on printing. I don't know how to debug in CodeBlocks yet, so I can't tell you what's hiding in the memory to solve this myself
Although this does not fix your code, you stated that you are currently learning C++, so I think providing a better solution is even much better.
Here is another simpler way to achieve your goal:
#include <iostream>
using namespace std;
int main()
{
unsigned int i, n;
cout << "What is your number?" << endl;
cin >> n;
for (auto i = 0; i < n; i++)
{
for (auto j = 0; j <= i; j++)
{
cout << "*";
}
cout << endl;
}
}
Remember, trying to implement an easy, simple and efficient way is always better for performance, readability and debugging. Good luck in your learning and happy coding!
The most easiest way to do this
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cout << string(i, '*') << endl;
}
return 0;
}
Related
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;
}
I'm new to C++ and using minGW version 6.3.0-1. I am not able to compile this code.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int r, c, a[5][5];
cout << "Test loop";
for (int x = 1; x <= 6; ++x)
{
cout << "Value of variable x is: " << x << endl;
}
cout << "Test loop ends" << endl;
cout << "Enter the number of rows and columns:";
cin >> r >> c;
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
cout << "Enter the array element:";
cin >> a[i][j];
}
}
cout << "The array you entered:" << "\n order:" << r << "x" << c;
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
getch();
}
Also please help find out which C++ standard im using currently.
return 0;
getch();
}
Your main() returns before waiting for a character using getch(). Switch those two lines:
getch();
return 0;
}
Also please help find out which C++ standard im using currently.
If you don't specify -std when you compile your code, gcc 6.3 defaults to -std=gnu++14 which means you are using C++14 with GNU extensions. See the documentation for further details.
This question already has answers here:
How to make cin take only numbers
(2 answers)
Closed 6 years ago.
So the requirements for this program is to be able to increment arrays of the same size (size from 5 to 15 indexes) and increment each element in the array by one using for and while loops. The last task is to take values from the first array and put them in reverse order and assign them to the second array.
So everything works as normal, and the program rejects invalid inputs and does not go into an infinite loop. However, the program accepts some inputs that are not wanted.
For example, I would input something like '12 a' or '7 asdfkla;j lasnfg jasklgn asfg' and it would go through. It is interesting too because the code registers only 12 or 7 and completely ignores the rest. I think it is because once it hits a non-integer character, it would stop ignore the rest.
Why is it ignoring the rest of the input? And is there a way to catch this error from going through?
Also, if you see anything that catches your eye, feel free to critique c: I am always looking to improving.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
int x;
int j = 0;
bool not_valid = true;
system("color f");
cout << "Program will ask for an input for the size of an array.\n"
<< "With the array size defined, program will generate semi-\n"
<< "true random integers from 0 to 8. First array will then\n"
<< "be assigned to the second in reverse (descending) order.\n\n";
do {
cout << "Enter array size (0 - 15): ";
cin >> x;
if (x >= 5 && x <= 15) {
not_valid = false;
cout << "\nArray size: " << x << endl;
}
else {
cout << "Invalid input.\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
} while (not_valid);
int *arr0;
int *arr1;
arr0 = new int[x];
arr1 = new int[x];
for (int i = 0; i < x; i++) {
arr0[i] = rand() % 9;
}
for (int i = 0; i < x; i++) {
arr1[i] = rand() % 9;
}
cout << "\nARRAY 0 (unmodified, for):\n";
for (int i = 0; i < x; i++) {
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 0 (modified, for):\n";
for (int i = 0; i < x; i++) {
arr0[i]++;
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 1 (unmodified, while):\n";
for (int i = 0; i < x; i++) {
cout << arr1[i] << "\t";
}
cout << "\n\nARRAY 1 (modified, while):\n";
while (j < x) {
arr1[j]++;
cout << arr1[j] << "\t";
j++;
}
int second = x - 1;
for (int i = 0; i < x; i++) {
arr1[second] = arr0[i];
second--;
}
j = 0;
cout << "\n\nARRAY 1 (array 0, descending):\n";
while (j < x) {
cout << arr1[j] << "\t";
j++;
}
cout << endl << endl;
system("pause");
return 0;
}
Take input in string and then check if it's a number or not.
Example:
#include<iostream>
#include<sstream>
#include <string>
using namespace std;
int main()
{
string line;
int n;
bool flag=true;
do
{
cout << "Input: ";
getline(cin, line);
stringstream ss(line);
if (ss >> n)
{
if (ss.eof())
{
flag = false;
}
else
{
cout << "Invalid Input." << endl;
}
}
}while (flag);
cout << "Yo did it !";
}
I need help understanding the difference in logic between the while loop / for loop here's the sample code :
#include<iostream>
using namespace std;
int main(void)
{
cout << "A multiplication table:" << endl
<< " 1\t2\t3\t4\t5\t6\t7\t8\t9" << endl
<< "" << endl;
for(int c = 1; c < 10; c++)
{
cout << c << "| ";
for(int i = 1; i < 10; i++)
{
cout << i * c << '\t';
}
cout << endl;
}
return 0;
}
I tried rewriting it as a while loop , but the outcome is missing information.
#include <iostream>
using namespace std;
int main() {
int i = 1;
int c = 1;
while (c< 10){
cout << c <<"|";
c++;
while (i< 10){
cout << i * c << '\t';
i++;
}
cout << endl;
}
cin.clear();
cin.ignore();
cin.get();
return 0;
}
Someone suggested that resetting i to 1 would give the rest of the results, i'm having trouble understanding why the while loop requires a reset whereas the for loop does not.
You would have to set i=1 to get your expected behavior in both examples. With the for loop, this is already taken care of since in the header of the for loop, there is a for(int i = 1; ...; ...).
for (i=0;i<n;i++) {
dosomething;
}
is equivalent to:
i=0;
while (i<n) {
dosomething;
i++;
}
The problem in your code is that you don't reset i to 1 in the inner loop.
Declare int i=1 inside the loop over c, not outside of it.
Try this:
#include <iostream>
using namespace std;
int main() {
int c = 1;
while (c< 10){
cout << c <<"|";
c++;
int i=1;
while (i< 10){
cout << i * c << '\t';
i++;
}
cout << endl;
}
cin.clear();
cin.ignore();
cin.get();
return 0;
}
I would like to analyze the complexity of my code algorithm.Therefore,i must have 2 different programs giving the same functions to allow me to start off.
Currently this is my own code.
I'm not sure if it is allowed that i would like to have someone that could volunteer his own way code to compute summation of factorial for me as the 2nd program code.
Preferrably a nested loop.
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++)
{
c = c * i;
a = a + c;
}
cout << "The sum of the factorials is " << a << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int val;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
static const int results[] = {
0, 1, 3, 9, 33, 153, 873, 5913, 46233, 409113,
4037913, 43954713, 522956313
};
cout << "The sum of the factorials is " << results[val < 0 ? 0 : val] << endl;
system("pause");
return 0;
}
Note that I replicated the defect in the original program which causes it to return the incorrect value if the user enters 0.
This alternate version assumes 32-bit integers because it takes advantage of overflow behavior. Extending to 64-bit integers is left as an exercise.
I do not understand what you do with another nested way but i hope this can help...
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++){
c *= i;
a += c;
}
int c2=1;
for (i = val; i > 1; i--){
c2*=i;
c2++;
}
cout << "The sum of the factorials is " << a << endl;
cout << "The sum of the factorials is " << c2 << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int suma = 0;
int n = 0;
cout << "Sum of factorials\n";
cout << "-------------------------------\n";
cout << "Insert number of n: ";
cin >> n;
int i = 1;
while (i <= n)
{
int factorial = 1;
for(int j=1; j<=i; j++)
{
factorial = factorial * j;
}
suma += factorial;
i++;
}
cout << "Sum of factorials is: " << suma;
system("pause");
return 0;
}