How to fill the separators between integer without the redundant one? - c++

I am trying to get the factors of positive integer. What I want is 8 = 2*2*2. However, what I get is *2*2*2. How can I get ride of the first *? Is there a standard way to better describe this situation?
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char separator = '*';
cout << "Input a positive integer: ";
cin >> num;
while(num !=1){
while((num % i) != 0){
i++;
}
cout << setw(2) << setfill(separator) << i;
num = num/i;
}
}
Input a positive integer: 8
*2*2*2

Use a separator that is updated. Start with "" and set to "*" thereafter.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char *separator = "";
cout << "Input a positive integer: ";
cin >> num;
do {
while((num % i) != 0){
i++;
}
cout << separator << i;
separator = "*";
num = num/i;
} while (num > 1);
}
Also changed to do loop to cope with num == 1 and num == 0 which print nothing in OP's original code. Code could use unsigned as a further extension/ protection.

One way to accomplish that is by outputting setfill(separator) only when num is not equals to 1.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char separator = '*';
cout << "Input a positive integer: ";
cin >> num;
while(num !=1){
while((num % i) != 0){
i++;
}
cout << setw(2) << i;
num = num/i;
if (num != 1)
cout << setfill(separator);
}
}

Related

I'm having issues with my for_each function and my finding prime function right now

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <typename t>
void find_prime(const vector<t>& V)
{
int num;
bool is_prime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
cout << "This is the prime number: " << num << endl;
}
else {
cout << "This number is not a prime number: " << num << endl;
}
}
int main() {
int input;
vector<int> num;
cout << "please enter a number larger than 1: ";
cin >> input;
for (int i = 0; i < input; i++) {
cout << i << endl;
}
num.push_back(input);
for_each(num.begin(), num.end(), find_prime<int>);
}
Right now I'm writing currently doing a project that has a user populate the arry with a singular number, and then display all the numbers from 2 up to the number the user entered. After doing so it should then step through the vector and pass each element of the array through the find_prime function. Though I do not know why my program is deciding it wants to start crashing.
This is a simpler implementation of your code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
std::string find_prime(int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return "This number is not a prime number: ";
}
}
return "This is a prime number: ";
}
int main() {
int input;
cout << "please enter a number larger than 1: ";
cin >> input;
for (int i = 2; i <= input; i++) {
cout << find_prime(i) << i << endl;
}
}
The above code takes in a number, and then for every number starting from 2 up to the number entered, the computer prints "This is a prime number: " <num> for primes and "This number is not a prime number: " <num> for non-primes.
Here, I have removed the usage of templates because that's completely unnecessary. For the same, I have also removed the vector you created. But in case you need all of that, you can use the below code, but I strongly advise the code provided earlier:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <typename t>
std::string find_prime(t num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return "This number is not a prime number: ";
}
}
return "This is the prime number: ";
}
int main() {
int input;
vector<int> nums;
cout << "please enter a number larger than 1: ";
cin >> input;
for (int i = 2; i <= input; i++) {
nums.push_back(i);
}
for (auto& i : nums) {
cout << find_prime(i) << i << endl;
}
}

How to countdown to 0 C++

I need to count down to 0. I am only printing 0 to the screen. How can I print all the count-down characters to the screen? Below is the code I am using right now.
#include <stdio.h>
#include <iostream>
using namespace std;
class Solution {
public:
int num;
int numberOfSteps (int num)
{
while (num != 0)
{
if (num % 2 == 0)
{
num = num / 2;
cout << num;
}
else
{
num = num - 1;
cout << num;
}
}
}
};
int main () {
int num;
Solution myObj;
cin >> num;
cout << myObj.num;
}
You're passing the num to std::cout. You are also not calling numberOfSteps(...) anywhere in your code.
Replacing the line with cout << myObj.numberOfSteps(num); fixes the problem, but a tidier solution would be as follows:
#include <stdio.h>
#include <iostream>
void countDown (int num) {
while (num != 0) {
if (num % 2 == 0) {
num = num / 2;
std::cout << num << std::endl;
} else {
num = num - 1;
std::cout << num << std::endl;
}
}
}
int main () {
int num;
std::cin >> num;
countDown(num);
}
Class is not necessary as there is no state and the function is void since it does not return anything.
I am revisiting this question and have created a simpler solution than my original post:
#include <iostream>
using namespace std;
int num;
int main()
{
cout << "Please enter the number you would like to count down to zero : ";
cin >> num;
while (num > 0)
{
cout << num << endl;
num--;
}
cout << "The number is now zero.";
return 0;
}

Converting an Ascii Character to an Integer

Hey guys I have a program here that basically needs a key to open encrypted data but for some reason every time I run the program when converting the Ascii numbers to characters I get characters with the wrong Ascii value. For example in the code below if it tried converting the Ascii Value '50' to a char I would get 'e' instead of '2'. Any help would be much appreciated.
#include "stdafx.h"
#include "iostream"
#include "string"
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
string key = "5053525055";
int asciiValues;
char asciiChars;
for (int i = 0; i < key.length(); i += 2)
{
asciiValues = (int)(key[i] + key[i + 1]);
asciiChars = (int)(key[i] + key[i + 1]);
}
Here's the complete code for those interested.
#include "stdafx.h"
#include "iostream"
#include "string"
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
void encryption(string text)
{
char asciiChar;
int asciiValue = 0;
string key;
string encoded;
srand((unsigned)time(0));
int random_integer = rand();
cout << random_integer << endl;
//Creates the key for the string.
for (int i = 0; i < to_string(random_integer).length(); i++)
{
int asciiValue = (char)(to_string(random_integer)[i]);
key = key + to_string(asciiValue);
cout << asciiValue << endl;
}
int help = to_string(asciiValue).length();
/*Function that converts the individual characters in the input
string to AsciiValues and then puts them into the encoded data.*/
for (int i = 0; i < text.length(); i++)
{
int asciiValue = char(text[i]) + random_integer;
encoded = encoded + to_string(asciiValue) + ".";
}
cout << "Encrypted data: " << encoded << endl;
cout << "Your key for this encoded data is " << key << endl;
}
void decryption(string text, string key)
{
char asciiChars;
int asciiValues;
int number;
string qkey;
string decoded;
/*for (int i = 0; i < to_string(random_integer).length(); i++)
{
int asciiValue = (char)(to_string(random_integer)[i]);
key = key + to_string(asciiValue);
cout << asciiValue << endl;
}*/
for (int i = 0; i < key.length(); i += 2)
{
asciiValues = (int)(key[i] + key[i + 1]);
asciiChars = (int)(key[i] + key[i + 1]);
number = asciiChars - '0';
cout << number << endl;
}
cin >> qkey;
}
int main()
{
string answer;
int question = 0;
string vkey;
ask:
cout << "Would you like to:\nEncrypt Data[1]\nDecrypt Data[2]\nExit[3]" <<
endl;
cin >> question;
if (to_string(question) != "1"&&to_string(question) !=
"2"&&to_string(question) != "3")
{
goto ask;
}
else if (to_string(question) == "1")
{
while (answer.length() > 1000 || answer.length() < 1)
{
cout << "Please enter a string that has a length of 1 to 1000
characters. ";
cin >> answer;
cout << endl;
}
encryption(answer);
cin >> answer;
goto ask;
}
else if (to_string(question) == "2")
{
cout << "Please enter the string you would like decrypted. ";
cin >> answer;
cout << endl;
cout << "Now please enter the key for the string. ";
cin >> vkey;
cout << endl;
decryption(answer, vkey);
}
return 0;
}
ASCII number characters begin at 30hex (or 48dec). Therefore '0' = 30hex. So, to get an ASCII character, you must add '0' (30h).
'cout << 5 + '0'; // 53dec (35hex)

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;
}

Program prints all digits from array, prints backwards

I'm making a program that prints all digits from an array (entered as an integer) and it works, but the digits are printed backwards and I don't know how to reverse them. Can someone help?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
cout << digit << '\n';
number /= 10;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
// numdigits = explode(n,digits);
cout << "[";
while (n > 0) {
int digit = n % 10;
n /= 10;
digits[digit] = digit;
cout << digits[digit];
}
cout << "]" << endl;
}
You just have to reverse the array using reverse() from <algorithm>.
Code:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
int array_c = 0;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
number /= 10;
array[array_c++] = digit;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
explode(n,digits);
reverse(digits,digits+array_c);
cout << "[";
for(int i = 0; i < array_c; ++i)
cout<<digits[i];
cout << "]" << endl;
}
Your use of
digits[digit] = digit;
is not right. You probably meant to use
digits[numdigits] = digit;
You can fix your problem by dividing the work into two steps. In the first step, you store the digits. In the second step, you print the digits.
int numdigits = 0;
while (n > 0) {
cout << "n: " << n << endl;
int digit = n % 10;
n /= 10;
digits[numdigits++] = digit;
}
// Make sure to print them in reverse order.
cout << "[";
for ( ; numdigits > 0; )
{
cout << digits[--numdigits];
}
cout << "]" << endl;