C++ :: Negative Numbers Not Working?
Jan 22, 2015
I have a program where the user inputs a line of numbers, and the two highest ones are displayed. It works fine, until negative values are entered at which point it shows 0 as the result.
Code: #include <iostream>
using namespace std;
int main( ) {
int num = 0;
int highest = 0;
[Code].....
View 6 Replies
ADVERTISEMENT
Nov 10, 2014
So I tried creating a test code for reading a negative number and positive number but whenever I enter a negative number it read it as being positive.
#include <stdio.h>
#include <iostream>
#include <iomanip>
[Code].....
PS: I am using char over int because the program that I am testing for requires me to use 8 bit variable.
View 2 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
Feb 15, 2014
So i have this program that takes in user input and stores them into an array and then prints them, removes duplicates, and sorts in ascending order. The user can also stop by inputting a sentinel value (in this case -1). But i am also supposed to ignore any negative value besides -1. When i input any other negative value into the program it messes up. How would i go about ignoring the negative values?
Code:
#include<stdio.h>
int main()
{
int input, nums[20], i, j, k, temp, count=0, count2=0;
for(i=0;i<20;i++)
[Code] .....
View 8 Replies
View Related
Jul 24, 2013
I've been working on this program to create a simple desk calculator for a school assignment, and I managed to finish. All we had to do was add, subtract, multiply, and divide positive integers - and I was able to do that just fine. This program got me thinking though, because I do not know how to write commands to multiply/divide negative numbers.
In fact, when I divide a number like 21 by 4, it comes out to 5 because I don't know how to allow it to compute remainders (which wasn't a requirement for my program). This intrigued me so I've been trying to figure it out for the last few days but to no avail. Here's my code:
Code: void flush_buffer(){
int ch;
while ((ch = getchar()) != '
' && ch != EOF);
[Code]....
And just know that my code works perfectly fine, I'm not here for troubleshooting it. I just want to know what I can change to allow negative values to be correctly computed.
View 12 Replies
View Related
Sep 2, 2013
I have a homework assignment due that told me for the "input specification" that "n" is an integer greater then 0. How would I put this in and where?
View 1 Replies
View Related
Mar 2, 2014
I am reading one of the exercise solutions for C Programming Language: The C Programming Language Exercise 3-4
In it, it states that negative numbers are biased by (2^n - 1) (i.e. -I is represented by (2^n - 1) - (+I). So:
Code:
Bias = 2^8 - 1 = 255 = 11111111
Subtract 25 = 00011001
Equals = 11100110
what is meant by the "bias" here and what is the value of "I" here. It just suddenly uses "I" without explaining what it is.
View 4 Replies
View Related
Jan 27, 2014
#include <iostream>
using namespace std;
int initialization (int []);
int identifying (int [],int);
[Code].....
View 8 Replies
View Related
Jul 28, 2012
Ok well i know that isdigit in my code is seeing the negatives as a character and so i made my program give an error message. But i need negative numbers to work how could i get this to work with negative numbers.
The code is supposed to add the 3 numbers input into the command line argument including negative numbers.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[]){
int add=0;
int i;
[Code] ...
View 2 Replies
View Related
Oct 21, 2013
I have a error with one of my programs. I'm supposed to get rid of negative numbers when there are numbers that are randomly generated. Here is the middle part of the code.
{
int vectorLength = 10;
vector<int> bothSigns(vectorLength);
cout << " Input vector: ";
for (int i = 0; i < vectorLength; i = i + 1)
{ bothSigns[i] = rand()%201 - 100;
[code] .....
The part where i'm supposed to start is after the /////'s. However, whenever I input a number for the random numbers(not put in part of code), i keep getting a segmentation error.
View 3 Replies
View Related
Mar 6, 2013
I have to write a program that uses negative numbers in a conjecture algorithm. The program terminates when a number is reached that was already outputted. I can not get it to work and I have been racking my brain for a week.
#include <iostream>
using namespace std;
int main() {
int x[1000], y[1000], i=0, j=0, count=0, w=0, z[1000], t = 0;
[code]....
View 2 Replies
View Related
Mar 1, 2014
I having some issues with two different programs here... One of them crashes and returns random negative numbers whenever it reaches a "fscanf" function and the other displays a "Polink fatal error: access denied" error.
Code:
/* Evan Wentz */
/* Pike - ET2560 */
#include <stdio.h>
#define MAX_ACCTS 100
int accounts [];
double balances [];
[Code] ....
I thought the reason this kept crashing before was because I didn't type the data into the text file it was writing too correctly, but I made another program to do that, and it crashed whenever it got to fprintf. Program works perfect besides the file stuff...
View 6 Replies
View Related
Nov 25, 2014
I got the while loop right... but how do i write the minimum and max to get negetive numbers like -88....
Code:
#include <stdio.h>#include <math.h>
#include <conio.h>
int gcd(int, int);
int main() {
int x, y, min, max, result, power, flag = 1;
char c;
[Code] ....
View 6 Replies
View Related
Nov 12, 2014
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototype
void sortArray(int array[], int size);
[Code] ....
This program was made to allow students to enter as many test scores as they want and the program will show them in ascending order and then will calculate the average of all test scores. It works wonderful until you enter a negative test score and then it throws the averaging process off. I can't seem to be able to make the program not accept the negative numbers.
View 1 Replies
View Related
Jun 19, 2014
So I have to make a program that allows the user to enter both positive and negative numbers and the program is suppose to calculate the sum of only the positive values while ignoring the negative values. Also it is to be a sentinel-controlled loop with a number ending the set of values.
View 4 Replies
View Related
Aug 27, 2014
Having error . I multiplied 0 by -4 and my result is -0 instead of 0. I tried to change the data type put It won't work. This is my code:
#include <iostream>
int main () {
double b, c;
std::cout<<"b: ";
std::cin>>b;
std::cout<<"c: ";
std::cin>>c;
std::cout<<b*c<<std::endl;
return 0;
}
View 2 Replies
View Related
Dec 11, 2013
im supposed to create a program that reads in a list of integers from the terminal and writes the negative numbers to one file and the positive numbers to another file.
i got most of it doen but for some reason its not writting the negative numbers. on what im doing wrong?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int pos_num = 0;
int neg_num = 0;
int positive_numbers = pos_num % 5;
[Code]...
View 2 Replies
View Related
Nov 18, 2013
I am trying to average the negative numbers and positive number and of course the total average.
This will read in a list of n values, where n is not known ahead of time. The number of values read into the array will be saved in n.
vector<int> readList() {
std::vector<int> result;
ifstream inFile;
inFile.open("setA.txt");
for (int x; inFile >> x; ) {
result.push_back(x);
[code]....
array is a one-dimensional array of integers and n is the number of elements in that array that contain valid data values. Both of these are input parameters to the function. The function must calculate 1) the average of the n integers in array, storing the result in ave; 2) the average of the positive numbers (> 0), storing the result in avePos, and 3) the average of the negative numbers (< 0), storing the result in aveNeg.
void avgs (std::vector &array, int &ave, int &avePos, int &aveNeg) {
int sum = 0, pos_sum = 0, neg_sum = 0, pos_count = 0, neg_count = 0;
for (auto i : array) {
sum += i;
if (i > 0) { pos_sum += i; ++pos_count; }
[code]....
View 1 Replies
View Related
Dec 2, 2014
I have a program that gets prime numbers that doesn't work. It looks like it works for all prime numbers except odd non-prime ones. Here is the code:
Code:
#include <iostream>
using namespace std;
bool isPrime(int prime) {
while(true) {
int track = 0;
[Code] ...
View 3 Replies
View Related
Jul 12, 2013
I have a c program that I partially have working. The problem is basically writing a program that allows the user to input the amount of calories they plan to eat a meal and disperse the calories from top to bottom. My program produces the output in the example if I enter 1050 but the issue I noticed if the number of calories is just enough to cover the burgers I get negatives in the other variables.
For example, if I enter a total amount of calories of 1050, I can eat: Output: 2 burgers @ 770 calories (1050 - 770 = 280 calories remain) 1 bag of pretzles @ 170 calories (280 - 170 = 110 calories remain) 1 pear @ 80 calories (110 - 80 = 30 calories remain) 6 tsp. ketchup @ 30 calories If I input 1050 I get the above output but if I input a different integer such as 2000 this is my output 5 burgers @ 1925 calories 0 bag of pretzles @ 0 calories -1 apple @ -80 calories -35 tsp. ketchup @ -175 calories I can't give the full code since this assignment holds a lot of points and was up all night getting it work.
So I'll provide pseudocode
define all 4 variables burger 385, pretzel 170, pear 80, ketchup 5 print out text How many calories can you eat prompt user input
Divide user input into burger How many burgers can bet eaten subtract calories eaten from original user input
Divide calories left into pretzel How many bags can bet eaten subtract burger calories from pretzel calories
Divide calories left after preztel into pear How many pretzels can be eatn subtract pretzels calories from pear calories
Divide calories left over into ketchup how much ketchup can i use show on screen (int total)of burgers @ (int calorie total) calories show on screen (int total)bags of pretzels @ (int calorie total) calories show on screen (int total)pears @ (int calorie total) calories show on screen (int total)teaspoons of ketchup @ (int calorie total) calories
The problem I see is that subtracting the calories from the pear from the left over calories of the pretzel calories leads to a negative. If leftover calories minus 80(pear int) its less then 0 . The calculations from the pear onward to ketchup become incorrect resulting in negative output.
View 6 Replies
View Related
Sep 3, 2014
I nead to define an a negative number a normal posetive number i defined like #define RSSI_UP 1 can i write #define RSSI_DOWN -1???
View 2 Replies
View Related
Sep 21, 2014
how to use '+', '-', '*' as preprocessor directives??
I want to do the following work.
#define + 10
#define - 20
#define * 30
View 4 Replies
View Related
May 2, 2013
What are positive and negative infinity for different data types in c++, are they represent maximum and minimum limit of a type? or positive infinity is not a finite value.can some explain this positive and negative infinity paradigm
View 3 Replies
View Related
Sep 30, 2013
The code on lines 44-53 is suppose to display a message when the user enter a negative number, however, when a correct positive number is entered the message is display again.
#include<iostream>
#include<cctype>
using namespace std;
int main() {
char carType;
int A, B, C;
[Code] ....
View 2 Replies
View Related
Mar 12, 2014
I am getting strings from an HTTP request that will have hex values and I must convert those strings to a signed decimal.
//typical string inside response: //0E1D052BFBB711C1002C0042007A014DFE44022B270F7FFF8000000000000000
//every 4 characters above are a signed decimal value
for (a = 0; a <= 63; a+=4){
sprintf(vval,"0X%c%c%c%c",response[a],response[a+1],response[a+2],response[a+3]);
ds = strtol(vval, NULL, 16);
sprintf(vval,"%d",ds);
}
The problem is I never see a negative number. Decoding 0x8000 gives me 32768 but not -32768.
View 7 Replies
View Related
Oct 9, 2013
I am using the first method, listed here.
I want to take the time measurement of a construction of a tree, which contains about 2841482 nodes (inner and leaves). Here it is:
Code:
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
Tree t(...);
clock_gettime(CLOCK_MONOTONIC, &end);
int64_t time;
time = timespecDiff(&end, &start);
std::cout<<"Time: " << time << " ns
"; which gives me always a negative value, like -13481628 ns.
View 9 Replies
View Related