Getting the previous and current number of sequence - c++

I have a problem on how to get the previous and current number of a sequence of integers . The problem is that everytime the previous number is same as the current (for example 15 12 16 10 9 8 0 and both are 8). Any ideas how to change it ? Thanks . Here is my code so far:
#include<iostream>
using namespace std;
int main()
{
int currentNumber;
int input;
int previousNumber = 0;
cin >> input;
while (input > 0 && input!=0)
{
currentNumber = input;
cin >> input;
previousNumber = currentNumber;
}
cout << previousNumber << endl;
cout << currentNumber << endl;
return 0;
}

If I have understood correctly then what you need is the following
#include <iostream>
int main()
{
int currentNumber = 0;
int previousNumber = 0;
for (int input; std::cin >> input && input > 0; )
{
if (currentNumber) previousNumber = currentNumber;
currentNumber = input;
}
std::cout << previousNumber << std::endl;
std::cout << currentNumber << std::endl;
return 0;
}

Related

How could I check If a variable is a certain number multiple times while changing what to do if It matches?

I want to check what the variable "num" is from 0 - 15 How can I check if it is one of those numbers and what number it is so far I've got.
#include <iostream>
using namespace std;
int num = 0;
int main()
{
cout << "Number to check:";
cin >> num;
{
if num = 0;
cout << "0000";
{
if num = 2;
cout << "0010";
{
if num = 1;
cout << "0001";
{
if num = 3;
cout << "0011";
{
if num = 4;
cout << "0100";
{
if num = 7;
cout << "0111";
{
if num = 5;
cout << "0101";
{
if num = 6;
cout << "0110";
{
if num = 8;
cout << "1000";
{
if num = 9;
cout << "1001";
{
if num = 10;
cout << "1010";
{
if num = 11;
cout << "1011";
{
if num = 12;
cout << "1100";
{
if num = 13;
cout << "1101";
{
if num = 14;
cout << "1110";
{
if num = 15;
cout << "1111";
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return 0;
How may I be able to do this If I can't, how can I convert a number to a 4-bit Binary byte? And if possible how can I make sure the number inputted is in the range of 0 - 15 and that there isn't letters in the inputted string
The program below does what you want.
#include<iostream>
#include <bitset>
int main() {
int num = 0;
std::cout << "Number to check: ";
std::cin >> num;
while (std::cin.fail() || num > 15 || num < 0) {
std::cout << "Error! Invalid Input \n";
std::cin.clear();
std::cin.ignore(256, '\n');
std::cout << "Number to check: ";
std::cin >> num;
}
std::bitset<4> value;
value = { static_cast<unsigned long>(num) };
std::cout << value.to_string() << "\n";
return 0;
}
Use <bitset>
#include <bitset>
#include <iostream>
#include <string>
int main()
{
std::string input{ "12" };
// std::cin >> input;
unsigned long number = std::stoul(input); // does number checking
if (number < 16) // do value checking
{
std::bitset<4> value{ number };
std::cout << value.to_string() << std::endl;
}
}
Since you declared num as an integer, your program won't work if you input nondigit characters during cin. I believe the shortest variant of your code without dealing with std::bitset would be
const int MAX_BITS = 4;
cout << "Number to check:";
cin >> num;
if (num < 0 || num > (1 << MAX_BITS) - 1) {
cout << "Invalid number, next time enter between 0 and " << (1 << MAX_BITS) - 1;
return 0;
}
for(int bit = MAX_BITS - 1; bit >= 0; --bit){
cout << ((num & (1 << bit)) > 0);
}
cout << endl;

How to fix this pyramid pattern in C++?

I'm trying to make this output:
This is my code, what I have so far:
#include <iostream>
using namespace std;
int main() {
int number;
bool flag;
do {
cout << "\t\t\Menu\n";
cout << "Enter a number between 6 and 12.\n";
cin >> number;
if (number > 5 && number < 13) {
flag = true;
for(int index = 1; index <= number; ++index) {
//Loop for spaces.
for(int spaces = index; spaces < number; ++spaces) {
cout << " ";
}
//Loop for numbers.
int counter = index;
int counter2 = 1;
for(int index2 = 1; index2 <= (2 * index - 1); ++index2) {
if (counter > 0) cout << counter--;
else cout << ++counter2;
}
cout << "\n";
}
} else cout << "Enter a valid number!\n";
} while (!flag);
return 0;
}
My output:
How to fix my output with proper spaces, I tried to concatenate with spaces but it doesn't fit good, how to fit it properly?
First, you want to output a few more spaces here:
for(int spaces = index; spaces < number; ++spaces) {
cout << " ";
}
To handle different number length well, I suggest C++'s equivalent of printf() and format string, cout << setw():
#include <iomanip>
cout << setw(4) << number;
... or just use printf:
printf("%4d", number);

Using an Array[10] and incrementing it with a While loop using modulo 10 to Pair User inputs together

#include <iostream>
using namespace std;
int main()
{
long int number;
int digits;
cout << "Enter Number: ";
cin >> number;
int counter[10] = { 0,0,0,0,0,0,0,0,0,0 };
while (number != 0) {
digits = number % 10;
counter[digits] = counter[digits] + 1;
number = number / 10;
}
for (int i = 0; i<10; i++) {
if (counter[i] != 0) {
cout << i << ": " << counter[i] << endl;
}
}
return 0;
system("pause");
}
I'm having an issue with my code that when I run it and enter a Number nothing really happens. It is supposed to run something like 1234556789 and the output should look like
1 : 9
2 : 8
3 : 7
4 : 6
5 : 5
I know sometimes if there isn't a system pause this happens where it runs part of the code and just ends, but I'm not sure whats wrong here.
#include <iostream>
using namespace std;
int main()
{
long int number;
int digits;
cout << "Enter Number: ";
cin >> number;
int counter[10]={0},a=0;
while (number != 0) {
digits = number % 10;
counter[a] = digits; //made changes to this line
number = number / 10;
++a;
}
for (int i = 0; i<10; i++) {
if (counter[i] != 0) {
cout << i << ": " << counter[i] << endl;
}
}
return 0;
}
All you are doing right now is printing how many digits there are of each number 0-9 in the number. If you want to pair elements together, then you can use std::vector and iterators. The number of digits in your input can be either even or odd and you would have to account for both cases.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long int number;
cout << "Enter Number: ";
cin >> number;
vector<int> digits;
if (number == 0)
{
digits.push_back(number);
}
while (number != 0)
{
digits.push_back(number % 10);
number /= 10;
}
auto it_begin = digits.begin();
auto it_end = digits.end() - 1;
if (digits.size() % 2 == 1)
{
for (; it_end != it_begin; ++it_begin, --it_end)
{
cout << *it_end << ": " << *it_begin << endl;
}
cout << *it_end << endl;
}
else
{
for (; it_begin < it_end; ++it_begin, --it_end)
{
cout << *it_end << ": " << *it_begin << endl;
}
}
}
With number = 1234556789, the output is:
1: 9
2: 8
3: 7
4: 6
5: 5
If you want first 10 no. Only use this code
include using namespace std;int main() {long int number;cout << "Enter Number: ";cin >> number;for (int i = 1; i<=10; i++) {cout << i << ": "<

How to add some numbers inputed by the user given a specific formula

So i need to write a program that asks for some numbers from the user (the amount of numbers is determined by the user) and then add them given this formula: ANSWER = FIRST - SECOND + THIRD - FIFTH + ...
where FIRST, SECOND, etc are the first, second and the rest of the numbers input by the user.
The problem is that i can create a loop that stores the numbers but actually, it only updates the value of the "num" variable. This is the code i have written.
#include <iostream>
using namespace std;
int main() {
int num, counter;
double answer;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
}
return 0;
}
Inserting a if-else clause that controls the remainder of the integer division of index i by 2 you can separate even and odd cases to obtain the desidered effect
#include <iostream>
using namespace std;
int main() {
int num, counter;
double answer;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
if(i%2==0)
answer+=num;
else
answer-=num;
}
return 0;
}
You can also do this assuming you don't need to store the numbers input by the user. What I am basically doing is just toggling between +1 and -1 that I then multiply by the number input by the user and then straightforwardly adding it to the answer.
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int num, counter;
double answer = 0;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
answer += num*pow(-1, i);
}
cout<<answer;
return 0;
}
You can also do:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int num, counter;
double answer = 0;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
if(i%2 == 0)answer += num;
else answer -= num;
}
cout<<answer;
return 0;
}

Moving elements in an array by certain amount

So I have a program that reads in a certain number of keys. An example would be
5 1 10 21 9 6 21 11 13 16 20
The text file example would be
Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2
I want my program to start at Java and depending on first key, which in this case is 5, would move over 5 elements leading to "red". And so on and so on. However, I have managed to read in all the data and put them into arrays, I am just having trouble figuring out how to move the pointer in the array.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct pieces {
char word[5];
int jump;
} ;
// Main Function
int main ()
{
// declare variables
int wordCount[2];
int keyCount[2];
int numKeys, numKeys2;
int numWords;
int keyAmount = 1;
int wordAmount = 23;
int keyarr[11];
pieces cypher[wordAmount];
char filename[10];
ifstream inData;
int temp[10];
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
inData.open(filename);
if(inData.is_open());
{
// read in data
for ( numKeys = numWords = 0; numKeys < keyAmount; numKeys++){
// read in num of words and keys
inData >> wordCount[numKeys] >> keyCount[numKeys];
//read in words followed by jump key
for( numWords = 0; numWords < wordAmount; numWords++){
inData >> cypher[numWords].word >> cypher[numWords].jump;
}
// read in the actual keys
for( numKeys2 = 0; numKeys2 < wordCount[numKeys]; numKeys2++){
inData >> keyarr[numKeys2];
}
}
//print out data
for( int j = 0; j < numKeys; j++){
cout << wordCount[j] << "\n";
cout << keyCount[j] << "\n";
}
cout << "\n";
for ( int i = 0; i < wordAmount; ++i){
cout << cypher[i].word << " ";
cout << cypher[i].jump << " ";
}
cout << "\nKeys: " << "\n";
for(int k = 0; k < 11; k++){
cout << keyarr[k] << " ";
}
cout << "\n";
}
inData.close();
return 0;
}