(C++) How to round decimals like 1.05000? - decimal

I have a problem where in my algorhithm I should round the result down to 5 decimal places, with the zeros included even after the last number.
The only test-case not working in my algorhithm is:
Input:
milk 1
4
bread
meat
milk
aaaaa
Output:
1.05000 // and my output displays 1.5
For an example; my result of 1.05 should display as 1.05000 or 1.2 as 1.20000. Now, the rest of the algorhithm is working fine, so the only problem is the rounding part:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char name[50];
cin >> name;
double price = 0;
cin >> price;
int N;
cin >> N;
char check_name[50];
double result = 0;
bool found = false;
double result_circle = 0;
int finally_found = 0
for (int k = 0; k < N; k++) {
cin >> check_name;
for (int i = 0; i < strlen(name); i++) {
if (name[i] == check_name[i]) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
finally_found++;
break;
}
}
if (finally_found > 0) {
result = price + (price * 0.05);
} else {
result = price + (price * 0.18);
}
// here is where the problem starts
result_circle = result * 1000000; //temporarily expanding the number to the 6th decimal place
if ((int)result_circle % 10 > 4) { // checking if the 6th decimal place is bigger than 4
result_circle += 10; // increasing the 5th decimal place
}
result_circle = (int)result_circle / 10; // removing the 6th decimal place which we were checking
cout << (int)result_circle / 100000 << '.' << (int)result_circle % 100000; // here is the main problem, where 105000 % 100000 is seen as 5000 not 05000
return 0;
}
I assume the main problem here is that ‘105000 % 100000 = 5000’ because the 0 after the decimal point is unfortunately left out.
If anyone could display the simplest way to fix this problem it would be great.

#include <iomanip>
.
.
.
cout << fixed << setprecision(5) << result;
This is the code that worked for me, answered by _Bob.
The full code:
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
int main()
{
char name[50];
cin >> name;
double price = 0;
cin >> price;
int N;
cin >> N;
char check_name[50];
double result = 0;
bool found = false;
double result_circle = 0;
int finally_found = 0;
for (int k = 0; k < N; k++) {
cin >> check_name;
for (int i = 0; i < strlen(name); i++) {
if (name[i] == check_name[i]) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
finally_found++;
}
}
if (finally_found > 0) {
result = price + (price * 0.05);
} else {
result = price + (price * 0.18);
}
cout << fixed << setprecision(5) << result;
return 0;
}

Related

C++ Console Application1.exe has triggered a breakpoint,Visual Studio 2015.Array and random numbers

I'm trying to run this code in VS 2015 c++, but I have this problem: while getting the correct answer, I also get the error C++ Console Application1.exe has triggered a breakpoint. My task is to randomly choose 10 numbers between 1 and 50, show these numbers and sort them by odd and even numbers. Thanks in advance.
#include<iostream>
#include<ctime>
using namespace std;
void main() {
const int size = 10;
srand(time(NULL));
int arr[size], odd = 0, even = 0;
for (int i = 0; i < size; i++)
{
arr[i] = rand() % 50 + 1;
cout << arr[i] << '\t';
}
cout << endl;
int *arrOdd = new int[odd];
int *arrEven = new int[even];
for (int i = 0; i < size; i++)
{
if (arr[i] % 2 == 0) {
arrEven[even] = arr[i];
even++;
}
else
{
arrOdd[odd] = arr[i];
odd++;
}
}
int a = 0, b = 0;
for (int i = 0; i < size; i++)
if (arr[i] % 2 == 0) {
cout << "arrEven = " << arrEven[a] << endl;
a++;
}
else
{
cout << "arrOdd = " << arrOdd[b] << endl;
b++;
}
system("pause");
}
int arr[size], odd = 0, even = 0;
//odd's and event's dont change
//...
int *arrOdd = new int[odd];
int *arrEven = new int[even];
So you are trying to allocate arrays with 0 length

My adding program outputs SUPER WEIRD

I have been trying to read two numbers as string, convert them into int vectors, then add them for my lab at school (I loop this process 4 times). I have run my code to find very strange results.
My code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void input(string &largeString1, string &largeString2);
void convert(string largeString1, string largeString2, vector<int> &largeInt1, vector<int> &largeInt2);
int asciiToInt(char ch);
void add(vector<int> largeInt1, vector<int> largeInt2, vector<int> &finalInt);
void output(const vector<int> finalInt);
int main()
{
string largeString1;
string largeString2;
vector<int> largeInt1(12, 0);
vector<int> largeInt2(12, 0);
vector<int> finalInt(13, 0);
for (int i = 0; i < 4; i++)
{
input(largeString1, largeString2);
convert(largeString1, largeString2, largeInt1, largeInt2);
add(largeInt1, largeInt2, finalInt);
output(finalInt);
}
system("pause");
return 0;
}
void input(string &largeString1, string &largeString2)
{
cout << "Input:" << endl << endl;
cin >> largeString1;
cin >> largeString2;
}
void convert(string largeString1, string largeString2, vector<int> &largeInt1, vector<int> &largeInt2)
{
int size1 = size(largeString1);
int size2 = size(largeString2);
for (int i = 0; i < size1; i++)
{
largeInt1[11 - i] = asciiToInt(largeString1[size1 - 1 - i]);
}
for (int j = 0; j < size2; j++)
{
largeInt2[11 - j] = asciiToInt(largeString2[size2 - 1 - j]);
}
}
int asciiToInt(char ch)
{
return (ch - '0');
}
void add(vector<int> largeInt1, vector<int> largeInt2, vector<int> &finalInt)
{
for (int i = 0; i < 12; i++)
{
if (largeInt1[11 - i] + largeInt2[11 - i] >= 10)
{
finalInt[12 - i] = finalInt[12 - i] + largeInt1[11 - i] + largeInt2[11 - i] - 10;
finalInt[12 - i - 1] = 1;
}
else
finalInt[12 - i] = finalInt[12 - i] + largeInt1[11 - i] + largeInt2[11 - i];
}
}
void output(const vector<int> finalInt)
{
cout << endl << "Output:" << endl << endl << "The sum is: ";
for (int i = 0; i < 13; i++)
{
cout << finalInt[i];
}
cout << endl << endl;
}
My outputs first output seems to give the correct answers, but the next 3 outputs do not add up (pun not intended). Also, how do I get rid of the annoying leading zeroes?

Write a snippet of C/C++ code that creates this list of base-12 numbers that count from 0000 to BBBB

Im not sure what to do i want it to print 0000 to ending in BBBB i was trying to use the printf statement anyways, if anyone can help me figure this out that would be great. Thanks
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char digits[] = "0123456789AB";
for (int column1=0; column1<=12; column1++) {
for (int column2=0; column2<=12; column2++) {
for (int column3=0; column3<=12; column3++) {
for (int column4=0; column4<=12; column4++) {
std::cout<< digits[column2]<<endl;
}
}}}
return(0);
}
The 4 for loops are not the prettiest thing ever, but they should work and I'm not sure it's worth the complications to do it differently. So keep what you have, just print all digits:
std::cout<< digits[column1]<< digits[column2] << digits[column3] << digits[column4]<<endl;
It's better to parametrize the base and the column count to avoid many nested for's.
#include <iostream>
const int columnCount = 4, base = 12;
char digitToChar(int digit) {
if(digit >= 0 && digit <= 9) {
return '0' + digit;
} else {
return 'A' + digit - 10;
}
}
bool increment(int* number) {
int currentColumn = columnCount - 1;
++number[currentColumn];
while(number[currentColumn] == base) {
number[currentColumn] = 0;
--currentColumn;
if(currentColumn < 0) {
return false;
}
++number[currentColumn];
}
return true;
}
void outputNumber(int* number) {
for(int i = 0; i < columnCount; ++i) {
std::cout << digitToChar(number[i]);
}
std::cout << std::endl;
}
int main() {
int number[columnCount] = {0, 0, 0, 0};
bool overflow = false;
do {
outputNumber(number);
overflow = !increment(number);
} while(!overflow);
return 0;
}

C, convert hex number to decimal number without functions

i'm trying to convert hexadecimal number to decimal number. What i've come up so far is:
#include <unistd.h>
#include <stdio.h>
long convert(char *input, short int *status){
int length = 0;
while(input[length])
{
length++;
}
if(length = 0)
{
*status = 0;
return 0;
}
else
{
int index;
int converter;
int result = 0;
int lastNumber = length-1;
int currentNumber;
for(index = 0; index < length; index++){
if(index == 0)
{
converter = 1;
}
else if(index == 1)
{
converter = 16;
}
else{
converter *= 16;
}
if(input[lastNumber] < 45 || input[lastNumber] > 57)
{
*status = 0;
return 0;
}
else if(input[lastNumber] > 45 && input[lastNumber] < 48)
{
*status = 0;
return 0;
}
else{
if(input[lastNumber] == 45)
{
*status = -1;
return result *= -1;
}
currentNumber = input[lastNumber] - 48;
result += currentNumber * converter;
lastNumber--;
}
}
*status = -1;
return result;
}
}
int main(int argc, char **argv)
{
char *input=0;
short int status=0;
long rezult=0;
if(argc!=2)
{
status=0;
}
else
{
input=argv[1];
rezult=convert(input,&status);
}
printf("result: %ld\n", rezult);
printf("status: %d\n", status);
return 0;
}
Somehow i always get resoult 0. Ia am also not allowed to use any other outher functions (except printf). What could be wrong with my code above?
This:
if(dolzina = 0)
{
*status = 0;
return 0;
}
is not merely testing dolzina, it's first setting it to 0. This causes the else clause to run, but with dolzina equal to 0 which is not the expected outcome.
You should just use == to compare, of course.

logic error in prime # program. The program just ends after I answer the prompt so I can't see the numbers

//Purpose: Display first 'n' (user chosen) number if emirps to the console, five per line.
//Note: An "emirp" is a prime number that is also prime when reversed.
#include <iostream>
using namespace std;
bool isPrime(int value); //Prototyle for "prime number function"
int reverse (int value2); //Prototype for "emirp function"
int main()
{
//Ask the user for a positive number
cout << "Please enter a positive number: ";
int n;
cin >> n;
//Reject negative value input
if ( n < 1)
{
cout << "INVALID NUMBER \n";
}
else
{
//Calculate all emirps up to 'n'.
int test = 0;
int number = 2;
while (test < n)
{
if (isPrime(number) && reverse(number))
{
cout << "\n" << reverse(number) << "\t\t\t";
test++;
}
else
{
test++;
}
}
}
system("pause");
return 0;
}
bool isPrime(int value)
{
//If value is prime, the remainder (count) will be zero twice--for 1 and itself.
int divisor = 1;
int count = 0;
int prime = 0;
if (value % divisor == 0)
{
count++;
++divisor;
}
if (count = 2)
{
return true;
}
else
{
return false;
}
}
int reverse(int value2)
{
//reverse the number
value2*=10;
value2 = value2 %10;
value2/=10;
//same procedure as prime function
int divisor2 = 1;
int count2 = 0;
int emirp = 0;
if (value2 % divisor2 == 0)
{
count2++;
++divisor2;
}
if (count2 = 2)
{
int emirp = value2;
}
return emirp;
}
How does this even build?
if (count = 2)
{
...
}
Also:
your reverse function just returns an int, what do you expect
if (isPrime(number) && reverse(number)) to do with that result?
It's not a good way of working to do the whole calculation again btw:
cout << "\n" << reverse(number) << "\t\t\t"; //you already did this in your "check"
Edit:
And no wonder it doesn't work.
You check the number-value (2) every time, not n
If it is just about viewing the console output :
Press CTRL+F5 to run application in Visual studio.
just provide a getch()in main() function
And your code syntax is not in a right way :
if (count = 2) //count ==2
{
return true;
}
if (isPrime(number) && reverse(number))
{
cout << "\n" << reverse(number) << "\t\t\t";
test++;
}
will call reverse() 2 times.
modify it something like ;
int RevNum = reverse(number);
if (isPrime(number) &&RevNum)
{
cout << "\n" << RevNum << "\t\t\t";
test++;
}

Resources