C++ :: How To Add Two Numbers In Own Datatype
Nov 27, 2013
I have made a new data type which named LongDouble
it takes 16 byte to save the number
the first bit for the sign
the next 15 bit for the exponent
the other 112 bit for the mantissa
I have to apply four operation on these numbers , but i have a problem in addition , when the sign for any of the numbers is negative ,how i can perform the operation , also if there is a Mantissa how i can also perform the operation?
if i try to save mantissa in a variable there would a loss of precision ... Here's my code
LongDouble LongDouble::operator+(const LongDouble& olong) {
LongDouble temp;
if (this->sign == 0 && olong.sign == 0) {
temp.exponent = this->exponent + olong.exponent;
[Code] .....
View 4 Replies
ADVERTISEMENT
Jun 6, 2013
I have designed a class called matrixType that has some overloaded operators (+, -, *, and <<); the arithmetic operator functions of which are overloaded as member functions of the class. As an alert mechanism, I want a message displayed when two matrices of dissimilar sizes are added/subtracted OR when two incompatible matrices are being multiplied. Displaying this error message is not the problem. However, I want a scheme where on detecting two matrices’ incompatibility, the operator function returns the error message (a string datatype) instead of what would be an erroneous result (the expected matrixType object).
In other words, what I may be essentially asking is: Is it possible for a function, say,
matrixType matrixType::operator+(const matrixType& otherMatrix) const
{
.
.
.
}
to return a dataType (like a string) other than the expected matrixType?
View 1 Replies
View Related
Nov 30, 2014
I just wanted to know a way to cut off any remaining zeroes from a double data type. I' trying to calculate cost and output it but it keeps adding a bunch of zeroes on the end. I know there must be a way to
View 1 Replies
View Related
Mar 31, 2013
the compiler doesnt accept void functions used with threads (for the first time and all of sudden!?)
#include <cstdlib>
#include <thread>
#include <windows.h>
[Code]....
this is a sigment of my code. Compiler says that the error occurs in line 47 (marked by ***). Leaving out the braces makes it even worse.
View 3 Replies
View Related
Nov 11, 2014
My code:
#include<cstdio>
#include<iostream>
using namespace std;
main() {
long double j;
scanf("%Lf", &j);
cout<< j;
return 0;
}
If I give any number as input, the output is always 0. why? where's the problem ? p
View 6 Replies
View Related
Feb 28, 2013
coming from Java, my experience with the Classes in C++ is quite limited.
Thats why I am having trouble converting the following (simple!) Java-Program.
Most examples with linked lists I found on the web describe how to implement the LinkedList class itself. But my problem is different: I want to use such a Class (I have a LinkedList class available on my system which is presumably OK).
Code: package main;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
BoxList<String> sl = new BoxList<String>(
new ArrayList<ABox<String>>());
sl.getList().add(
new BoxPair2<String>(new BoxPair2<String>(
[code].....
View 10 Replies
View Related
Aug 4, 2013
My Fraction.h class looks like :
class Fraction {
int num;
unsigned int den;
public:
Fraction(int = 1,int =1);
//Constants of Datatype
[Code] ....
The implementation Fraction.cpp is as follows :
#include "Fraction.h"
Fraction::Fraction(int n, int d):num(n),den(d){
cout << This is double param constructor <<endl;
}
And the application main.cpp is
int main(){
Fraction f1(3,9);
f1 = Fraction::sc_fUnity; // how to implement this ?
}
How can I write the Fraction.cpp for the constant static member ?
View 6 Replies
View Related
Aug 11, 2014
How you would overload an operator for Common Data-types like "char" and "int".
I often use bool arrays to create a multilevel-trigger-systems, when iterating over multiple containers or waiting for two events to occur at the same time.
For example:
I would define..
bool trigger[2] = {0, 0};
And when doing work via a loop, I use it like so:
while(trigger[0] != 1 && trigger[1] != 1)
You can probably see where I'm going with this. I want to be able to use my bool array with the "!" operator.
So if "trigger == 0" (as a whole), it returns false.
How can I achieve this?
Can you create custom operators? Say if I wanted to create "or-gates" or "xor-gates" etc.
View 3 Replies
View Related
Apr 3, 2013
How to use static in a class, function and variable.
View 1 Replies
View Related
Mar 16, 2013
I am need to compare 2 items from 2 separate drop down lists. These values are of int type. Say for example 2 drop down lists showing years. Now i need to find out these 2 years selected is greater or nearer to the current year.
I tried converting the values i receive from the drop down lists to int variables. But i am getting error then. I gave it like below.
int tmp = Convert.ToInt32(ddl_1.SelectedItem.Text);
this gives an error like this - Input string was not in correct format.
I need to know how to put an item from a drop down list to a variable of int data type.
View 1 Replies
View Related
Oct 20, 2012
I have a class that is a template, I have to declare it in my main but i want the user to choose what type of data they will use in the class, I cant just declare myclass my, i have to use myclass<int> my, how can I change so user can select the proper datatype to use in the class.
View 5 Replies
View Related
Dec 20, 2014
I have the codes for such a problem where, to create a program that counts how many times the second string appears on the first string. Yes it counts if you put 1 letter only, but if you put 2, it is an error. As an example. If the first string is Harry Partear, and the second string is ar, it must count as 3. Here's the code:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
[Code] ....
View 6 Replies
View Related
May 1, 2014
How to do the problem below using loop?
Input numbers until the user types a 0, then output the product of the non 0 numbers: e
E.g., if the user types 2 4 6 0, the program should output 48
View 3 Replies
View Related
Jun 20, 2013
User enters sentence "The Smiths have two daughters, three sons, two cats and one dog." (The numbers may change depending on what the user chooses to enter. He told us the range would be from zero to nine.) and we have to convert the written numbers within the sentence into actual decimal numbers and print out the new sentence. Ex. The Smiths have 2 daughters, 3 sons...etc.
I have written the following bit of code which reads the string and finds all the "written numbers" but I am not sure how to proceed from there. I am stuck on how to print out the new sentence with the converted numbers as my professor mentioned something about creating the new string using dynamic memory allocation.
Code:
#include <stdio.h>#include <string.h>
int main () {
char A[100];
int length = 0;
int i;
[Code] .....
View 7 Replies
View Related
Apr 18, 2014
Find all the prime numbers between a given pair of numbers. Numbers should be read in from an input file called "numbers.txt" and find all the prime numbers between them. Store the prime numbers in an array, then sort the array from greatest to least. Display the array before and after the sort.
I'm stuck on how to put the prime numbers into an array.
The input file has the numbers 1 & 100.
Here's what I have so far.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
fin.open("numbers.txt");
[Code] .....
View 1 Replies
View Related
Dec 7, 2013
Question: How to find a duplicate numbers and numbers found once in array.
View 7 Replies
View Related
Feb 4, 2015
I'm working on this program that I have to design a class Numbers that can be used to translate whole numbers to the English description of the number.
Now this is what I got so far:
#include <iostream>
#include <string>
using namespace std;
class Numbers {
private:
int number;
static string ones[];
static string tens[];
[Code] ....
The program seems to work. However its not giving me the right number description,
Example:
Please enter the amount you would like translated into words: 5
six dollars
please enter another number: 10
eleven dollars
please enter another number: 20
thirty dollars
please enter another number: 30
forty dollars
please enter another number: 100
two hundred dollars
please enter another number: 150
two hundred sixty dollars
please enter another number: 500
six hundred dollars
please enter another number: 1000
two thousand dollars
please enter another number:
View 4 Replies
View Related
May 15, 2013
ignoring negative numbers when I am trying to add up only positive numbers.
SAMPLE:
if (num>=0) {
sum= sum + num;
}
else
how would the else in this case being a negative number not be included in the sum
View 4 Replies
View Related
Dec 22, 2013
I need a list generated of all possible subset combinations for the set 1,2,3,4,5,6,7,8,9,10,12. Select six. Numbers cannot repeat.
Example subset: 1,2,3,4,5,6 (six selected, no repeats).
Example of what I dont need: 1,1,2,2,3,3,4,4,5,5,12,12 or 1,1,1,1,1,6.
I will also need the opposites removed, meaning...if I have 1,3,5,7,9,11 then I need 2,4,6,8,10,12 eliminated from the final list.
This is for a game, where you must select all numbers right or no numbers right.
View 2 Replies
View Related
Sep 21, 2014
The code below will generate combinations of numbers from 1 to 25 in an 15 numbers array. The only filter I've applied is that the sum of all the numbers in the vectors divided by 15 needs to be between 13 and 14.
I would like to count how many consecutive numbers there are in one combination, so that later i can apply another filter.. for example:
1 3 4 5 6 8 10 13 14 16 17 18 19 20 25
3 + 4 = 1
4 + 5 = 1
5 + 6 = 1
13 + 14 = 1
16 + 17 = 1
17 + 18 = 1
18 + 19 = 1
19 + 20 = 1
_____________
Count = 8, in this case..
I think it's not very difficult to do, but i just can't see how to do it.
#include <iostream>
#include <vector>
#include <numeric>
[Code]....
View 3 Replies
View Related
May 20, 2014
I want to make a program to print the product of even numbers between 1 and 30 and sum of odd numbers between 1 and 30. But the answer of product is negative. The photo shows the output of the code.
#include <stdio.h>
#include <conio.h>
void main ()
{
int i, even_product=1, odd_sum=0;
for(i=1;i<=30;i++) // For loop starts here!
[Code]...
View 5 Replies
View Related
Jan 25, 2015
So i have made an array and made a scanf in a for loop so it can store all numbers entered from keyboard in the array but i dont know how to put the numbers that are less than 5 in another array and then print that array out and the lenght of the array.
View 11 Replies
View Related
Apr 28, 2013
I wrote a program which sends a starting and ending range to other processes and the processes calculate the prime numbers in that range and return the count of prime numbers to the head process, process 0. But this is not working properly at the moment. I realize I still have to split up the range based on how many processes I have...I still have not figured out how I want to set that up. I
Code:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int isPrime(int num);
int main(int argc, char **argv){
}
[code]....
View 7 Replies
View Related
Mar 27, 2013
Write a program which reads a stream of numbers from a file, and writes only the positive numbers to a second file. The user should be prompted to enter the names of both the input file and output file in main(), and then main() will open both files. Another function named process() must then be called to read all the numbers from the input file and write the positive numbers to the output file. Note that you must pass the open stream variables for each file as arguments to the process() function, and that you need to (always) double check that the files opened successfully before using them.
This is what I have so far but its not working out!
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int process(ifstream &inf, ofstream &outf);
[Code] ....
View 1 Replies
View Related
Aug 2, 2013
All Numbers are random.
This will be the output
Enter Number: 4
Enter Number: 3
Enter Number: 2
Enter Number: 1
Enter Number 6
The Sum of all even numbers is: 12
The Sum of all odd numbers is 4
View 18 Replies
View Related
Oct 4, 2013
i have trouble with comparing two of the biggest numbers out of four numbers. Im working on an assigment with a dice game where i need to tell the computer the following;
"if the biggest number out of dice_three and dice_four is the same number as the biggest of dice_two and dice_one, then loop1=true"
this is how i have been writing it so far ;
if
(MAXA(dice_three,dice_four) == MAX(dice_two,dice_one)){
lopp 1=true;
}
(as seen) i am using:
#define MAX(dice_one,dice_two) ( (dice_one) > (dice_two) ? (dice_one) : (dice_two) )
#define MAXA(dice_three,dice_four) ( (dice_three) > (dice_four) ? (dice_three) : (dice_four) )
to calculate which one is the biggest number out of the two pares. The code i have been writing so far seem to have the wrong syntax.
View 6 Replies
View Related