C++ :: Program That Add / Subtract / Multiply Or Divide Two Integers - Incorrect Answers
Oct 7, 2012
Create a program that adds, subtracts, multiplies, or divides two integers. The program will need to get a letter (A for addition, S for subtractions, M for multiplication, or D for division) and two integers from the user. If the user enters an invalid letter, the program should display an appropriate error message before the program ends. If the letter is A (or a), the program should calculate and display the sum of both integers. If the letter is S (or s), the program should display the difference between both integers. When calculating the difference, always subtract the smaller number from the larger one. If the letter is M (or m), the program should display the product of both integers. If the letter is D (or d), the program should divide both integers, always dividing the larger number by the smaller one."
And here is the test data. I am posting the results from my desk-check table.
operation first integer second integer answer
A 10 20 30
a 45 15 60
S 65 50 15
s 7 13 6
G -1
M 10 20 200
d 45 15 3
d 50 100 2
Then, I transferred my program into a source file. Here it is:
//Exercise16.cpp - display answer of two integers
#include <iostream>
using namespace std;
int main() {
//declare variables
int firstInteger = 0;
[Code] ....
After putting in the data, everything worked fine, except the last two operations, which are M (multiplication) and D (division). All the answers for the last two operations essentially give me a 0.
View 4 Replies
ADVERTISEMENT
Oct 19, 2014
Program: I have 2 arrays: 1 for the correct answers to a quiz, 1 for the user. I then have a vector to hold the incorrect answers. It keeps outputting what looks like alt characters, why.
Here is the code:
#include <iostream>
#include <vector>
using namespace std;
int main()
[Code] .....
View 2 Replies
View Related
Feb 26, 2013
I am new to C programming and I am just wondering how to multiply / divide two different variables which the user type in as the promt is asking like this:
Code:
void inmatning3 (double *a, double *b) {
printf("Mata in tv217 stycken flyttal: "); /* asks you to type in 2 numbers */
scanf("%lf %lf", a, b);
}
When you've enterd the two numbers I need to eather multiply or divide the two variables "a" & "b" .....
View 5 Replies
View Related
Mar 28, 2014
I want to multiply and divide 2 string that contains number. For example
s1=4 and s2=8 s1*s2=32 ,s2/s1=2
I don't want convert string to integer and do it.because i deal with big numbers.and these should be in string form.
View 1 Replies
View Related
Aug 24, 2013
I'm trying to get my C program to compile but it's not working at all. I've programmed a little in C++ before but I'm not used to C. Here's my program so far:
Code:
int main(void){
// Establishes variables
int num1, num2, product;
float quotient;
[Code] .....
It keeps giving me an error message as follows:
"/usr/bin/ldrelabwk2: file format not recognized; treating as linker script
/usr/bin/ldrelabwk2:1: syntax error
collect2: ld returned 1 exit status"
View 11 Replies
View Related
Jan 8, 2015
Using two-dimensional arrays, write a program which multiplies an mxn matrix of integers by an nxr
matrix of integers.
INPUT FIRST (2x2) MATRIX:
Type in 2 values for row 1 separated by spaces: 3 4
Type in 2 values for row 2 separated by spaces: 5 7
INPUT SECOND (2x2) MATRIX:
Type in 2 values for row 1 separated by spaces: 1 1
Type in 2 values for row 2 separated by spaces: 2 2
3 4
5 7
TIMES
1 1
2 2
EQUALS
11 11
19 19
View 1 Replies
View Related
Feb 2, 2015
My program uses a while loop to eventually get to an error of zero and a root of sqrt(3). I'm not understand why after the third iteration the program fails to compute a new x value. I'm using Visual Studio 2013. The code tag instructions were dubious.
Code:
#include <stdio.h>
#include <math.h>
main() {
/*This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots.*/
printf("This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots. Enter your estimate of the root.
");
float x,y,z;
int num;
num = 0;
[Code]...
View 1 Replies
View Related
Mar 20, 2013
I'm practicing so I wrote this simple program that suppose to add and divide two numbers. It does that but the result comes out with a 0 at the front and don't know why.
#include<iostream>
using namespace std;
int main()
{
int a ,b;
int result;
a = 0;
b = 0;
[Code]...
View 4 Replies
View Related
Jan 10, 2014
I have to write a program, that multplicates very large numbers (out of range of long int). It's said that i need to use arrays and read the numbers as strings. My problem is to end function called "mnoz:, because i don't know how to sum the multiplicated values of arrays a and b.
Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include "winbgi2.h"
#include<string.h>
#define roz 10000
char lan(char a[])
[Code] ......
View 1 Replies
View Related
Feb 2, 2015
This program has to convert an unsigned binary number into a decimal number. No matter what binary number I enter, however, it always outputs that the decimal number is 0.
My code is as follows:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
string binarynumber;
cout << "Enter an unsigned binary number up to 32 bits." << endl;
[Code] ....
And my output:
Enter an unsigned binary number up to 32 bits.
00001111
That number in decimal is 0
The output should have shown the binary number in decimal to be 15, and I cannot find my error.
View 6 Replies
View Related
Feb 15, 2014
I have this simple program below:
Code:
#include <stdio.h>
#include <limits.h>
unsigned int rightrot(unsigned int x, unsigned int n) {
/* calculate number of bits in type */
size_t s = sizeof(x) * CHAR_BIT;
[Code] ....
The result it prints is 2684356604 on my 32-bit computer. The result I expect is as follows:
0xFF94 is 0000000000000000 1111111110010100 in binary.
Shift it right by 5:
0000000000000000 0000011111111100
Then take that result in shift it right by 27 (s is 32 and p is 5, so the difference is 27):
1111111110000000 0000000000000000
Now we use bitwise or:
0000000000000000 0000011111111100 | 1111111110000000 0000000000000000 = 1111111110000000 0000011111111100
That in decimal is 4286580732. So how does it come up with 2684356604?
View 2 Replies
View Related
Oct 28, 2014
I've been typed out a C program to let the user define the size of their string , and key in characters for this string , the program would then prompt the user for a character to search for in the string and return it's index value. Eg. Index of c in abc is 2. My code is as shown:
#include<stdio.h>
#define SIZE 20
int search(char x[SIZE+1] , int n , char s);
int main(void){
char x[SIZE+1] , s;
int n , index;
[Code] ....
However , after I key in my characters for the string , the program does not prompt me to input a character to look for, it just prints it out and returns some funny number. But the program works just fine is I move this portion to the top :
printf("Enter alphabet to find: ");
scanf("%c",&s);
View 1 Replies
View Related
Dec 17, 2013
Currently I have to manually look up values on several different tables in different locations. I have 8 or 10 tables with 100 to 500 parts.
I would like to write a program so if I enter a value it will return the corresponding correct answers from the tables.
If I entered 2.5 for a value it would return the following three items from the tables since they all meet the requirement.
Table 2
Part min max
235 2.4 2.9
Table 6
Part min max
589 2.3 2.5
Table 7
Part min max
12 2.3 2.7
What would the best method be for setting up and accessing tables like this? Is C++ good at representing something like this or should I be looking at a different language?
View 3 Replies
View Related
Nov 3, 2012
The following program takes user input into two arrays and should then determine how many items are different by comparing them with a loop. The comparison always show zero correct answers.
#include <iostream>
using namespace std;
const int QUESTION = 20;
//class for testing grades
class TestGrader {
[Code] ....
View 4 Replies
View Related
Feb 20, 2015
how do I subtract this so that I use the smallest number of $20, $10, $5, and $1 to pay $93? The book tells me to "Divide the amount by 20 to determine the number of $20 bills needed, and then reduce the amount by the total value of the $20 bills. Repeat this for the other bill sizes."I think the part in bold throws me off. What IS the total value of the $20 bills if I use $93 as the example? Is it $80, the highest we can go with $20 bills? Is it 4, the number I got from 93/20 after cutting off the decimal places, also the total number of $20 bills I can use?
I know how to do it and can do it in real life. It's like, "Oh, I need to pay $93. So I use four $20 bills, which brings me to $80. Using one more would bring it to $100, which is too much. So now, I need to use a $10 bill, which brings it to $90. Oh, so now, I need to use three $1 bills, which brings me to $93."But how do I tell that to a computer? It seems like I would have to put some kind of limits on it, or string code lines together,Here is the code so far:
Code:
#include <stdio.h>int main(void)
{
int amount, twenty, ten, five, one;
printf ("Enter a dollar amount:
");
scanf ("%d", &amount);
}
[code]....
View 5 Replies
View Related
Mar 12, 2014
I am looking for simple code that subtract two time interval. I have time t1=5hour 30 minute and other time in 24 hour format. This code i written but it not working as expected. it not printing 5:30 minute subtract
Code:
main() {
intTime1;
intTime2;
int hour=10;
int minute=5;
int second=13;
int h;int m;
doubleNtime;
[Code] ....
View 3 Replies
View Related
Nov 24, 2013
Here's my code :
>>>>>
std::vector<std::string> x = split("3 5", ' ');
int total = 0;
// then we loop over the elements
for(size_t i = 0; i < x.size(); ++i) {
// convert the string to an integer
int n = atoi(x[i].c_str());
total = total + n;
}
std::cout << "total = " << total << std::endl;
}
<<<<<
So, as you can see, this will add 3 to 5. However, I would like it to do the inverse (3 - 5). How I can do that?
View 2 Replies
View Related
Mar 16, 2013
this function subtracts a given value for each element in an array. I If the array is: hellohowareyou, and the value is 6 then result will be : 6 characters from each letter in the initial array
and the prototype of it is:
void minusFromArray(char arr[], int size, int value);
how do i convert each element in the array into ascii and subtract the values from those ascii characters?
View 1 Replies
View Related
Mar 29, 2014
I have a code for subtract of two char arrays(size of each is 50).but i want to write this program with string and i should be consider that size of each string may be not equals.for example we have s1=4777 and s2=55.and we should be subtract them. This is my code.
char* sub(char x[50],char y[50]) {
int i,j,temp=0;
char z[50]={'0'};
for(i=0;i<50;i++)
[Code] .....
View 9 Replies
View Related
Mar 17, 2013
I have to write a function Subtracts val from each character in the array
// If the array is: abcdefghi and value = 5, then result is: 5 ascii characters subtracted from the given array
// note: Make sure the character remains in the ' '
// to '~' (inclusive)
// If character < ' ', then add 95
// If character > 126, then subtract 95
// to put back within the range, and repeat as needed
// all printable characters are between this range
// ' ' is the 1st printable character (32)
// '~' is the last printable character (126)
note: subtracting values from the char will cause an overflow.
the prototype is: void subtracting(char array[], int size, int value)
View 3 Replies
View Related
Apr 24, 2013
Ok so I am currently trying to add and subtract two sets of integer arrays. When I run my program, the program does not subtract the numbers from the first set with the numbers from the second set. Could anyone here take a look at my code and help me figure out what the problem is?
#include <iostream>
#include "SafeArray.h"
using namespace std;
[Code].....
View 1 Replies
View Related
Dec 2, 2014
I compiled a simple program using a function to add two integers, in order to understand the difference of calling by value vs by reference.
Code:
#include <stdio.h>
int add(int a, int b, int sum);
int main()
{
int a, b, sum;
printf("Please enter two integers
");
scanf("%d%d", &a, &b);
[Code]...
I used these two (one, two) pages I found in the net while studying the subject. So here are my questions...
(a) Do I understand correctly that in the above code all a, b and sum are passed by value?
(b) If I wanted only sum to be passed by reference, how the above code would change?
(c) If I wanted all (a, b, sum) to be passed by reference, how the above code would change?
View 12 Replies
View Related
Sep 7, 2014
-Create three files namely all_numbers, odd_number and even_number.
-Get the number of records and the numbers to store from the user.
-Save all the numbers in "all_numbers" file.
-Read each number and check whether it is odd or even.
-Store the odd numbers in odd_number file and even numbers in even_number file.
-Finally, print the numbers stored in odd_number and even_number files.
The output of the program will look like this.
How many records that you want to store :41
2
3
4
The data are written too the respective files.
The even numbers are 2 4
The odd numbers are 1 3
View 9 Replies
View Related
Oct 6, 2014
I created a program in C that opens a text file and read integers.
File `numbers.txt`:
> 5 6 4 6 3 4 1
I use some restrictions with the `fscanf()` library and read the file, just for the record, and if everything goes fine I proceed to the next part of code (I know I could use `fget()`, but this is how I done it).The next part of the code will be this:
- First, I want the program to read the numbers from right to left. I look for an answer in google and many C courses, but I couldn't find a working solution. I think that the code is pretty simple, though I can't find it. **To be clear for the next part, we read the numbers from right to left.**
- Then, this is the hard part for me: I want the program to read the numbers and if the next number is higher (**not equal**) than the last one to keep it in memory, **only if it's higher than the last.** This can be continue for **1 through 500.000 numbers.**
- Then when the procedure done, I want to print how many numbers are greater than the last one and save it to a file, which I can do it if I solve the last problem.
We have the below numbers:
5 6 4 6 3 4 1
The program starts to read the numbers from right to left.Now which and how many numbers are greater than the last one?
The answer is 3 numbers:
5 6 4 [6] 3 [4] [1]
Then the program will print the number 3 with a message and save it to a file called `xxx.txt`. I am posting the `main` code, not all the code just what you need to solve the problem:
Code:
#include <errno.h> // macros for reporting and retrieving error conditions
#include <stdio.h> // load main C library function
#include <stdlib.h> // need that for many reasons ;)
#include <string.h> // loads various of characters functions
}
[code]....
In conclusion, I want 2 pieces of code:
- One to read the numbers from right to left
- One to make the above procedure
View 11 Replies
View Related
Mar 23, 2013
Would it be possible (and how) to use extremely large integers in a C++ program (such as 20 million digits)?
View 7 Replies
View Related
Dec 9, 2014
Add a program that declares two arrays of 5 integers each. Provide one function to load one array with random numbers from 1 to 25, another to fill the other array with user supplied values, and a third function to print an array. The main function should declare the arrays and use the functions to fill each array and print them both.
The code i have so far is
#include <cstdlib>
#include <iostream>
using namespace std;
void printaaray(int printarray1[], int printarray2);
[Code]....
View 2 Replies
View Related