C :: Largest Negative Number In 2 Complement
Mar 3, 2014
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].....
View 2 Replies
ADVERTISEMENT
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 30, 2013
find inserted numbers, let say 10 numbers and find the largest and second largest number with their position in an array?
View 3 Replies
View Related
Apr 28, 2014
Write a program that uses two functions; one finds the largest number and second largest number; and second function finds the average. The data, comprising of 20 different temperature values, is available on a file.
View 1 Replies
View Related
Jan 28, 2014
I am working on an assignment about converting an integer number to its 2's complement presentation. The binary representation is consisting of a single linked list.
As we all know, the steps of this converting is to taking the reminder of the absolute value, then flipping the 1 to be 0, and the 0 to be 1 in the binary number. And the last step will be to add 1 to the binary number invers.
I wrote a code that implements every thin correctly. However, when I reached the part of adding 1, the program was hanged.
int Absolute; //the first step is to convert the number to the binry reprezentation
Absolute = abs (value);// by take the Absolute value of the negative number, then find the
while (Absolute !=0) //the binary reprezentation {
int res;
res= (Absolute % 2);
pushFront (res);
Absolute /=2 ;
[Code] .....
View 11 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
Oct 10, 2014
I have the code working on converting a decimal number to base 1 through 16. I need getting the code to output the one's and two's complement.
Code:
#include <stdio.h>
int main(void) {
char base_digits[16] =
{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int converted_number[64];
long int number_to_convert;
int next_digit, base, index=0, a;
/* get the number and base */
[Code] ....
View 8 Replies
View Related
Nov 20, 2014
I am attempting to write code that receives a number from the user and outputs the largest prime number underneath the user's number. There are no errors in my code, but no matter what number is imputed, the program says the largest prime number is 1. I cannot find where the issue is in the code. Here is the code I wrote:
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int num);//function prototype
[Code] ....
View 2 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 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
Sep 26, 2013
i want to print Largest number from any 5 rows.Th number printed should be any one of the largest in the five rows of 2d arrays.I have created code for largest number in each row but how to pick and print them randomly?.
Code:
#include<conio.h>
main( )
{
int a,b,c,d,e,x;
int arr[] = {a,b,c,d,e};
int Matrix[5][5] ={ /*Initializing array*/
2,4,3,5,9,
6,8,2,2,10,
[Code]...
View 9 Replies
View Related
Jul 26, 2013
Its a code to find the largest number of a 3X3 matrix.The logic seems to be right........
Code:
#include<stdio.h>
main() {
int matrix[3][3],i,k,j,*a;
a=&matrix[0][0];
printf("Enter the elements");
for(i=0;i<=2;i++) {
[Code]....
View 7 Replies
View Related
Oct 12, 2014
I want to find third largest character in ascii as alphanumeritic. The problem is i cannot use arrays. How can I find third largest number ? Have to I compare all chars with each other ?
void third_largest(){
char ch;
do{
scanf("%c",&ch);
}
while(ch!=' ');
}
The chars can be hold up to press space ...
View 1 Replies
View Related
Feb 10, 2014
I need to calculate the expected time it takes to do an activity using a formula provided and after the loop is broken it needs to show the number of projects processed and the project with the longest expected time. I've got everything working except for finding the the project with the longest expected time. What I have here just keeps displaying the number of the last project processed regardless of if it was the largest or not, specifically lines 54-61 is what i can't seem to figure out.
#include <stdio.h>
int main( void ) {
unsigned int counter;
int projectnumber;
int optimistictime;
int realistictime;
[Code] .....
View 1 Replies
View Related
Oct 18, 2014
Write a program that will find the smallest, largest and average of the values in a collection of N positive integer numbers.
View 12 Replies
View Related
Dec 6, 2013
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?
View 2 Replies
View Related
Aug 30, 2013
While finding the primes , I do not know how to make it into one array so that...
View 7 Replies
View Related
Mar 23, 2013
What is the largest prime factor of the number 600851475143 ?
This is what I have. If I replace 600851475143 by 1000 (for example), it works fine until 2 divides 1000 to 500, 250, 125...but when it has to find the next divisible prime (ie 5) it cannot do that and the program stops working.
Code:
#include<iostream>
using namespace std;
bool find_prime(int number) {
int factor, num;
factor = 2;
num = number;
[Code] .....
View 1 Replies
View Related
Mar 10, 2013
I am trying to find the largest prime factor of a number. But when I am trying to determine if a number is a prime number in the function:
int is_prime(int number), I am unable to exit the loop.
Here is the code:
#include<iostream>
using namespace std;
int is_prime(int number) //the problem is in this function {
int num = number;
int factor=0;
do{
num++;
for(int i=1;i<(num+1);i++){
[code].....
So when the program runs, it first divides 20 by 2, to get 10, then divides 10 by 2 to get 5. Since, // condition 1 is not met, it passes 2 to the function int is_prime(int number). The function is able to return 3, but cannot exit the loop when num is 4.
I think the problem lies in the function: int is_prime(int number).
View 5 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
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
Jul 15, 2014
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] ....
View 9 Replies
View Related
Mar 6, 2015
How to find the largest and smallest number entered by a user. So far I'm able to add, find the average and count how many numbers are entered.
I'm just having trouble with find the max and min. I tried if statements and it breaks the program will it wont let me exit the while loop or the program will do a force close after entering a value.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
int maxNum=0, minNum=0, value, count=0, sum=0;
double average;
[Code] ....
View 4 Replies
View Related
Feb 27, 2013
I made the code that stores and prints 5 row by 5 column values but how to find the largest number from them.What should I do to find the largest number?If I use if-else then it would be very tedious but I think there is way out with for loop but still I can't frame the logic. Here is the code
Code:
#include<stdio.h>
main() {
int lnum[5][5];
int i,j;
for(i=0;i<=4;i++) {
[Code] .....
View 7 Replies
View Related
Sep 10, 2013
I am stuck looking at whats the problem ....
#include <iostream>
using namespace std;
int main() {
float average, largest = 0, smallest = 0;
int count = 1;
int num;
[Code] .....
View 1 Replies
View Related