C/C++ :: Dividing Two Integers Will Return Integer Value?
Dec 2, 2014
So I have a simple calculator that does a few operations (+ - * / %) Pretty basic stuff
I declared int x, y for the numbers, char operation, and float result.
the code is based on switch(operation)
The program is running alright, but when I divide 8/7 it returns 1 as the result, I tried changing the x and y to float but that won't work because of the case '%'
I also tried making local float variables in the case'/' but it won't compile "E2126 Case bypasses initialization of a local variable"
How can I make the division work and return a float value?
i am relatively new to C programming so i run into problems on daily basis. But this time i have something i just cant figuer out and i was hoping you could point me towards the right track. I am trying to divide two integers.DevValue by KpTotal. for some reason my micro controller allways crashes.Y is a variable of a distance measuring sensor. i have a 4x4 keypad to enter a three digit number (e.i 123) so Kp1 = 1 Kp2 = 2 Kp3 =3.
Code:
int kp1, kp2, kp3, kpTotal = 0; char txt[6] = "" int keypadPort at PORTD; sbit LCD_RS at RB4_bit; sbit LCD_EN at RB5_bit; }
[code]....
i think it has something to do with the format of the value. i read that the micro controller crash when dividing by zero.
How i can concatenate two integers into one integer. I have code that concatenate two integers but if the 2nd integer is zero it won't work. How can i modify it so that it can cater the case of y=0 too.
Code:
int concatenate(int x, int y) { int pow = 10; while(y >= pow) pow *= 10; return x * pow + y; }
Write a program that reads four integers from a file ‘input.txt’.
The program will then create a single integer number from the four integers. The output of the program will be the single number and single number + 600.
#include <iostream> using namespace std; int main () { int number , a; cout<<"enter en integer number"; cin>>number; cout<<"add 600 to the number" cin>>a=number+600; return 0; }
what I need is to get the first integer from a file and assign it to a variable and the others integers to an array. Example: Thats my file content 5 4 6 7 8 0 and that would be the code:
I got everything in this code running except for my remove function. What the project does is adds or removes an integer to a chain of integers created by the user. My add function works the first time but after that if I try to remove or add I believe it is pointing to the improper location and I don't know how to fix this....
Here is my code:
Header: // adds "number" to the array pointed to by "arrayPtr" of "size". // Note the size of the array is thus increased. void addNumber(int *& arrayPtr, int number, int &size); // removes a "number" from the "arrayPtr" of "size". // if "number" is not there -- no action
Return the digit at the user specified index of an integer. If the integer has n digits and the index is NOT in the range 0 <=index <n return -1 Start the digit numbering at 0. Example, if user input is 4 (index) and the integer equals 123456790 the return value for the function is 5 (start index at 0) ; if user input is 40 (index) and the integer equals 123456790 the return value for the function is -1
#include <iostream> #include <istream> #include <cstdlib> #include <cassert> #include <string> using namespace std; int getIndex(int, int);
I know that if I just use "return" by itself the warning goes away but fails to exit when the error occurs. I also believe this may not be the correct use of stderr. But I need the program to exit when an error has occurred.
Objective: Write a function with the given signature that will take a sorted array of integers and return the array compacted. That is, given an array containing: 1, 2, 6, 8, 8, 8, 9, 10, 10, when the function returns, the contents of the array should be: 1, 2, 6, 8, 9, 10.
Restrictions: Cannot use Distinct method
Signature: public static int[] CompactArray(int[] input)
I am having problem in writing the code for the problem "To assign the elements of 1-D integer array into 2-D array of integers such as if the array is 1,2,3,4,5,6 The resultant 2-D array should be like :
I wrote the following code to divide 100 customers into three clusters but it kept on hanging during execution. I used while loop. Attached is the text file to use. and the code below :
i made a calculator that you have to give terms and operators one by one but now i want to improve it. the user now should be able to type something like 5*78+325/sin(3*pi)-1 and the program should be able to calculate it. but i don't know how to use the signs '*','+','-' and '/' as delimiters and turn the input string into a string array. i mean the string "5*78+325/sin(3*pi)-1" will become a string array like "5,*,78,+,325,/,sin(3*pi),-,1,"
I'm supposed to store the value of a countrys population. Then gather out the percentage that countrys population holds when compared with the global population.
Anyway here's the code:
Code: #include <iostream> long swe_pop = 9644864; int main ()
[Code] .....
The result I'm getting is 0%.
I was under the impression that long (or long long) integers could hold high values. And that I could then divide these and answer with a float type value. Giving space for the decimals.
Im using the remquo function in the cmath library as follows:
int quotient; double a = remquo (10.3, 4.5, "ient);
This gives the correct remainder (a = 1.3) and quotient (quotient = 2).
Infact about 50% of the answers are right when I play around, however, trying something like:
int quotient; double a = remquo (2.25, 1.5, "ient);
yields an incorrect quotient of 2 and remainder of 0.
I do think this has something to do with float arithmetic. I recall tinkering with the float number 0.500 and that the CPU actually saves it as 0.50000000000000231. However if my suspicion of float arithmetic as the suspect is correct, I do not understand why a tenth decimal would make such a drastic difference as changing the quotient result.
I'm a beginner at c++ and I need to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. The program cannot tell the user how many integers to enter. I need to have separate totals for the even and odd numbers. what would I need to use so that I can read whatever number of values the user inputs and get the sum of even and odd?
I'm writing a program to calculate a final grade by adding 4 numbers minus the lowest grade and dividing by 3. My knowledge in c is not extensive I thought that a simple assigment operator would do the job but I'm getting a strange large numbers in the execution.
I'm writing some functions pertaining to binary trees. I've used recursion once before while learning quicksort but am still quite new and unfamiliar with it. And this is my first time touching a binary tree. So my question: In my addnode function, will the return root statement at the end ever return a value other than the value passed to the function?