so im creating a vector with unlimted inputs but i want it so that when the user wants to stop inputing data, they just enter a non numerical key and then just move on to the next vector which is just the first one but reversed and multipled by 8 and 100 added to it but when it should move on, it just adds a bunch of zeros as inputs
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
vector<int> (myVec1)(n);
vector<int> (myVec2)(n);
int i;
int sum;
for(i=0;i<n;++i){
cout<<"please enter an integer ";
cin>>myVec1.at(i);
cout<<endl;
if(cin.fail()){
for(i=0; i<n; i++){
cout<<myVec1.at(i)<<" ";
}
}
}
cout<<endl;
for(i=0; i<n; i++){
sum=(myVec1.back()*8)+100;
myVec1.pop_back();
cout<<sum<<" ";
}
return 0;
}
this is the result and as you see theres just a bunch of zeros after i tried to stop the inputs if any one can help me id really appreciate it :)
please enter an integer 435
please enter an integer 412
please enter an integer 43
please enter an integer 56
please enter an integer 2
please enter an integer q
435 412 43 56 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 116 548 444 3396 3580
int n has undefined value (not initialized)
Instead, ignore defining n and initialize your vector like this: vector<int> myVec1;
and when you read values, append them in the vector using myVec1.push_back(valueFromKeyboard);
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> myVec1;
vector<int> myVec2;
int valueFromKeyboard;
cout << "please enter an integer ";
while (cin >> valueFromKeyboard) {
if (!cin.fail()) {
myVec1.push_back(valueFromKeyboard);
}
cout << "please enter an integer ";
}
for (int i = 0; i < myVec1.size(); i++) {
cout << myVec1[i] << " ";
}
cout << endl << endl;
for (int i = 1; i < myVec1.size()+1; i++) {
myVec2.push_back(myVec1.end()[-i]*8+100);
}
for (int i = 0; i < myVec2.size(); i++) {
cout << myVec2[i] << " ";
}
return 0;
}
myVec1.end()[-1] is the last element of the vector, myVec1.end()[-2] is the second to last etc. That's why I created this loop for (int i = 1; i < myVec1.size()+1; i++) to go from 1 to myVec1.size()+1
Related
The task:
Write a C++ program which will print (half pyramid) pattern of natural numbers.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
I have tried using this code, but its not giving the output:
#include <iostream>
using namespace std;
int main()
{
int rows, i, j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
OUTPUT:
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
j in your inner loop is the 1 based index of the element in the current row (1,2,3 etc.).
Instead of printing it, you should print a counter that is increased over all the iterations.
Something like:
#include <iostream>
int main()
{
int rows, i, j;
std::cout << "Enter number of rows: ";
std::cin >> rows;
int n = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
std::cout << n << " ";
n++; // for next iteration
}
std::cout << "\n";
}
return 0;
}
Output example:
Enter number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
I was tasked with this problem for homework for my c++ class and I can't figure it out.
The task is: Create a program that will create a pattern in which is a pyramid. The user should enter the maximum number of rows to be output. Use a while loop that confirms the number of rows is between 1 and 9 inclusive. Next 1 should be output in the first row, 222 output in the second row, 33333 should be output in the third row, etc. For example if the user entered 7 the following would be output.
The code I have now does this almost exactly, instead of outputting, for example 222 for the second row, it outputs 2 2
Here is what my code looks like:
#include <iostream>
using namespace std;
int main()
{
int rows, count = 0, count1 = 0, k = 0;
cout << "Please enter the number of rows." << endl;
cin >> rows;
while (rows > 9)
{
cout << "That is an invalid selection, please choose up to 9 rows." << endl;
cin >> rows;
}
for (int i = 1; i <= rows; ++i)
{
for (int space = 1; space <= rows - i; ++space)
{
cout << " ";
++count;
}
while (k != 2 * i - 1)
{
if (count <= rows - 1)
{
cout << i << " ";
++count;
}
k++;
}
count1 = count = k = 0;
cout << endl;
}
}
Any help is appreciated, I'm assuming it should just be a small tweak.
This loop
while (k != 2 * i - 1)
{
if (count <= rows - 1)
{
cout << i << " ";
++count;
}
k++;
}
count1 = count = k = 0;
does not make sense. For example the variable count1 is never used except the statement
count1 = count = k = 0;
So it is unclear what is the purpose to define this variable.
Using manipulators from the header <iomanip> you can write a pyramid using only one loop.
Here is a demonstrative program
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
const int MAX_HEIGHT = 9;
std::cout << "Enter the height of a pyramid not greater than "
<< MAX_HEIGHT << " (0 - exit): ";
int height;
if ( not ( std::cin >> height ) or ( height <= 0 ) ) break;
if ( MAX_HEIGHT < height ) height = MAX_HEIGHT;
std::cout << '\n';
for ( int i = 0; i < height; i++ )
{
std::cout << std::setw( height - i ) << std::setfill( ' ' ) << i + 1;
std::cout << std::setw( 2 * i + 1 ) << std::setfill( char( i + '1' ) ) << '\n';
}
std::cout << '\n';
}
return 0;
}
Its output might look the following way
Enter the height of a pyramid not greater than 9 (0 - exit): 1
1
Enter the height of a pyramid not greater than 9 (0 - exit): 2
1
222
Enter the height of a pyramid not greater than 9 (0 - exit): 3
1
222
33333
Enter the height of a pyramid not greater than 9 (0 - exit): 4
1
222
33333
4444444
Enter the height of a pyramid not greater than 9 (0 - exit): 5
1
222
33333
4444444
555555555
Enter the height of a pyramid not greater than 9 (0 - exit): 6
1
222
33333
4444444
555555555
66666666666
Enter the height of a pyramid not greater than 9 (0 - exit): 7
1
222
33333
4444444
555555555
66666666666
7777777777777
Enter the height of a pyramid not greater than 9 (0 - exit): 8
1
222
33333
4444444
555555555
66666666666
7777777777777
888888888888888
Enter the height of a pyramid not greater than 9 (0 - exit): 9
1
222
33333
4444444
555555555
66666666666
7777777777777
888888888888888
99999999999999999
Enter the height of a pyramid not greater than 9 (0 - exit): 0
I am writing a program to sort a user entered array. The following code takes 8 single digit integers to be entered and then put into an array.
#include <iostream>
#include <stdio.h>
using namespace std;
void printArray(int ary[], int n) {
cout << "The sequence you entered is as follows:" << endl;
for (int i=0; i<n; i++) {
cout << ary[i] << " ";
}
cout << "\n";
}
int main() {
using namespace std;
int nums[8];
cout << "Please enter 8 single digits between 1 and 9" << endl;
for(int k=0; k<8; k++) {
scanf("%d", &nums[k]);
}
printArray(nums, 8);
cout << endl;
return 0;
}
But when I input digits, only the first integer is pushed into the array with the rest being 0s.
For example, when the input is:
2,5,7,2,1,4,6,8. The output is: 2 0 0 0 0 0 0 0
The input syntax you should use is like that : 2 5 7 2 1 4 6 8.
or you can change the scanf("%d", &nums[k]); to scanf("%d,", &nums[k]); and now you can enter 2,5,7,2,1,4,6,8
My input .txt looks as follows:
6 3
0 1 5
0 2 1
0 5 53
From the second line onwards I want to store the columns in arrays, so I did the following:
main(int argc, char** argv)
{
std::ifstream infile("instance1.txt");
int NumOne, NumTwo;
if (infile.good()){
infile >> NumOne >> NumTwo;
}
int Array1[NumTwo];
int Array2[NumTwo];
int Array3[NumTwo];
for(int i = 1; i < NumTwo + 1; i++){
infile >> Array1[i-1] >> Array2[i-1] >> Array3[i-1];
}
infile.close();
cout<<"first number"<<NumOne<<endl;
cout<<"second number"<<NumTwo<<endl;
for (int i=0; i < sizeof(Array1); i++){
cout << Array1[i] << " " << Array2[i] << " " << Array3[i] << endl;
}
cout<<"first array"<<Array1<<endl;
cout<<"second array"<< Array2<<endl;
cout<<"third array"<< Array3<<endl;
}
My output is the following:
first number6
second number5
0 1606413088 0
0 1 5
0 2 1
0 5 53
6923 5 1606414340
1 0 32767
1606673872 0 1606413088
32767 0 1
1606594089 0 2
32767 0 5
0 1 4
65793 2 5
80256 6923 5
0 1 0
80256 1606673872 0
0 32767 0
17623816 1606594089 0
1 32767 0
first array10x7fff5fbfeb10
second array20x7fff5fbfeaf0
third array0x7fff5fbfead0
Does anyone know where these numbers come from? I'm new to C++ and appreciate any hints about what goes wrong here.
I am not sure how this code already compiled but:
Issue One:
cout<<"first array"<<Array1<<endl;
cout<<"second array"<< Array2<<endl;
cout<<"third array"<< Array3<<endl;
I think this will display the hexadecimal address of each array, since you didn't type which element Array[?] in every array you are tying to display.
Issue Two:
for(int i = 1; i < NumOne + 1; i++){
infile >> Array1[i-1] >> Array2[i-1] >> Array3[i-1];
This code will set junk data, since it's going to try reading NumOne + 1 means 6 + 1 = 7 lines as max while you only have 3 lines. So what are you trying to do here?
Issue Three:
you need to set a fixed size before declaring any array. So int Numone = ? or Array[?]
Try this part of using vectors if you need to set the size of your container during run time
#include <vectors>
#include <iostream>
int main(int argc, char** argv)
{
std::ifstream infile("instance1.txt");
int NumOne, NumTwo;
if (infile.good()){
infile >> NumOne >> NumTwo;
}
std::vector<int> MyVectorOne(NumTwo);
std::vector<int> MyVectorTwo(NumTwo);
std::vector<int> MyVectorThree(NumTwo);
for(int i = 1; i < NumTwo + 1; i++){ // again this is wrong. What do you want here??
infile >> MyVectorOne[i-1] >> MyVectorOne[i-1] >> MyVectorOne[i-1];
}
infile.close();
cout<<"first number"<<NumOne<<endl;
cout<<"second number"<<NumTwo<<endl;
for (int i=0; i < MyVectorOne.size(); i++){
cout << MyVectorOne[i] << " " << MyVectorTwo[i] << " " << MyVectorThree[i] << endl;
}
// and here? what are you trying to display? this is also wrong
cout<<"first array"<< MyVectorOne<<endl;
cout<<"second array"<< MyVectorTwo<<endl;
cout<<"third array"<< MyVectorThree<<endl;
return 0;
}
I am a beginner in programming.
I am currently writing a code that takes the number 1 to 9 of rows and columns from the user. for single digits, there should be a "0"
the output should look like this:
Type a row number between 1 and 9: 3
Type a column number between 1 and 9:7
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
This is the code i currently have:
#include <iostream>
int main() {
int r,c,i,j,n,k;
cout<<"Type a row number between 1 and 9: ";
cin>>r;
while (r<1 || r>9){
cout << "Please enter a number between 1 and 9.";
cin>>r;
}
cout<< "Type a column number between 1 and 9: ";
cin>>c;
while (c<1 || c>9){
cout << "Please enter a number between 1 and 9.";
cin>>c;
}
n=r*c;
for(k=0; k<n; k++){
}
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
cout<<i<<" ";
}
cout << endl;
}
return 0;
}
I've added validity statements for the users.
This outputs:
Type a row number between 1 and 9: 3
Type a column number between 1 and 9:7
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
I can't figure out the 0 and how to implement the numbers into the rectangle.
Please help.
You're close, just change your output loop to:
int counter = 1;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
cout << counter << " ";
++counter;
}
cout << endl;
}
If you would like to avoid the extra local variable you can also just do:
cout << (i*c + j + 1) << " ";
I would prefer the explicit version using counter as it makes it obvious what you're doing and you can easily switch the order of the loops/output if desired.