C :: How To Define Negative Number
Sep 3, 2014I 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 RepliesI 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 RepliesHaving 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;
}
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] ....
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.
In this exercise:The C Programming Language Exercise 3-4..It states the following: "In a two's complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2 to the power (wordsize - 1)) ."
A char is one byte (255 bits). The range of an 8 bit variable using a two's complement representation is -128 to 127. Therefore -128 is the largest negative value. The statement in book suggests that the itoa function will not output -128 if we pass -128 as a parameter, because in itoa when we try to convert -128 to positive -128, the inverse of -128 is -128. However, I just ran this code in my computer and it successfully outputted -128.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 10
void reverse(char s[])
{
int c, i, j;
}
[code].....
I receive a negative number but in big endian order. ntohl seems work for unsigned only.
Is there a method for me to translate it back to the original negative number from big endian?
I am trying to run a simulation with a large number of objects (mainly arrays and vectors). I am not sure where shall I define my objects: inside or outside of the main() function, like the following two structures:
(1) ---------------
//main.cpp
int main(){
array<double, 1000> a_1 = {};
array<double, 1000> a_2 = {};
......
func_1(a_1, a_2, ..., a_100);
return 0;
}
[Code]...
I know there is a question about scope. But besides this question (which seems have no difference between these two structures here), is there any difference in terms of execution performance or security issue?
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]...
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.
I am trying to find the max number entered by the user, and it should terminate when a negative number is entered. For my code, it will just end when the user inputs a lower number than the previous. i.e.- 10 20 15 "The highest number is 20" when it should be "10 20 5 40 15 -1" "The highest number is 40". No arrays or do/while loops either.
#include <iostream>
using namespace std;
int Max(int x);
int main() {
int x;
[Code] ....
Why my program is returning a negative number at the end...attached is the program:
/*Write a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of 1 element.*/
#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
float recursiveMinimum (int ARRAY[], int n);
[Code] .....
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.
here's one more thing id like to do to make the input even better able to handle user error, but im not sure if its possible or at least easy. I need the function to return a large positive number. As of right now, it can handle users entering characters, but what if the user enters a negative number? is there a way to check to see if what is coming in is negative before the sign gets lost in conversion to unsigned"ness"?
Code:
unsigned long getNum(char prompt[80])
{
unsigned long darts;
printf("%s", prompt);
while((scanf("%lu", &darts)) != 1)
{
[code]....
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].....
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.
how to use '+', '-', '*' as preprocessor directives??
I want to do the following work.
#define + 10
#define - 20
#define * 30
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 RelatedI am trying to understand how to get the following value from a define. The code i am trying to understand is as follows:
Code:
#define RADIO_CONFIGURATION_DATA_RADIO_DELAY_CNT_AFTER_RESET {0xF000}
Code:
#define RADIO_CONFIGURATION_DATA {
Radio_Configuration_Data_Array,
RADIO_CONFIGURATION_DATA_CHANNEL_NUMBER,
RADIO_CONFIGURATION_DATA_RADIO_PACKET_LENGTH,
RADIO_CONFIGURATION_DATA_RADIO_STATE_AFTER_POWER_UP,
RADIO_CONFIGURATION_DATA_RADIO_DELAY_CNT_AFTER_RESET,
RADIO_CONFIGURATION_DATA_CUSTOM_PAYLOAD
}
and also have this
Code:
typedef struct {
U8 *Radio_ConfigurationArray;
U8 Radio_ChannelNumber;
U8 Radio_PacketLength;
U8 Radio_State_After_Power_Up;
U16 Radio_Delay_Cnt_After_Reset;
U8 Radio_CustomPayload[RADIO_MAX_PACKET_LENGTH];
}
tRadioConfiguration;
so then in the api i have the following
Code:
for (; wDelay < pRadioConfiguration->Radio_Delay_Cnt_After_Reset; wDelay++); .
so my question is how do i declare my variables, correct terminology ?, to achieve this.
I have one small doubt in arrays. Is it possible to define arrays like this a[b[10]] ?...
View 6 Replies View RelatedI want to define variable that is in HEX value & has 64 bits.but I dont know what I can use.....I use unsigned long long but it doesn't useful.
View 2 Replies View RelatedI 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.
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] .....
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.
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 RelatedI 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.
I am studying c and I thought what would be better than using my pi to play with relays and c. I am used to PHP as a scripting lang and don' t do much programming. So I wrote this to use wirepi and ask the user "on or off" they type on or off and it does it. it does work but I know something is wrong when I use strcmp in the if statment I can -10 for a value. Here is the code.....
Code:
#include <stdio.h>
#include <string.h>
main(int argc, const char * argv[]) {
char oo[100];
system("gpio mode 0 out");
printf("Do you want it on or off?
[Code] ....
This is what it outputs at the prompt
Do you want it on or off?
on
on
It is now on!
How can I just get Code: if (oo = on) {} and like so with off.