C :: Comparing Double To Integer

Feb 24, 2013

Given this code

Code:

double x=1.00,y=2,z=4;
if (y/z||++x)
x+=y/z;
printf("%f
",x); So (y/z||++x)

is true if at least one expression is true, that is either (y/z)!=0 or (++x)!=0 or both. I wonder how the comparison is done? Is (y/z) be truncated to integer or 0 be promoted to double?

View 2 Replies


ADVERTISEMENT

C++ :: Runtime Speed Integer Versus Double

May 27, 2013

Using integers faster than using floating points? Or more precise: what do you estimate as the chance that writing a custom floating point class is worth the effort?

I need to examine some C++ code for an ARM7 or ARM9 processor. Instead of using floating points, the coder had chosen to use integers only. To be able to fake floating points, a custom class was written. The coder states that he avoids using floating points as these are much how much slower. "Have you measured this?", I asked. "No, but it is known to be so", he replied.

Google taught me that most people state integers are faster than using doubles, because their operations (addition, multiplication) take less assembler instructions. But if you need those floating points, what do you estimate as the chance that writing a custom floating point class is worth the effort?

View 2 Replies View Related

C++ :: Comparing Char Pointers To Integer Pointers

May 21, 2013

I am a little confused while comparing char pointers to integer pointers. Here is the problem:

Consider the following statement;
char *ptr = "Hello";
char cArr[] = "Hello";

When I do cout << ptr; it prints Hello, same is the case with the statement
cout << cArr;

As ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e.
int iArr[] = {1, 2, 3};

If I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??

View 2 Replies View Related

C :: Assign Integer Value To Unsigned Char Array But It Is Not Storing Integer Values

Oct 25, 2013

I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet

Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}

The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.

View 9 Replies View Related

C++ :: Changing Integer Into New Integer With Simple Mathematical Operations?

Jun 15, 2014

changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).

View 4 Replies View Related

C/C++ :: Converting 8-byte Integer To String Based Integer

Oct 15, 2014

Complete the function myitohex so that its converts 8-byte integer to string based hexadecimals.

Ex: printf("0x%s",myitohex(64,hex)); should printout "0x40" on the screen
char *myitohex(uint8_t i, char *result){
???
return result;
}

I wrote:

#include <stdio.h>
#include <stdint.h>
char *myitohex(uint8_t i, char *result){
*result = i + '0';

[Code] ....

0xp gets printed out which is wrong. I think *result = i + '0'; is wrong. What changes should i do on this row ?

View 4 Replies View Related

C :: Comparing Password With Strcmp

Oct 7, 2013

I'm a novice with C programming and i have to solve an error in the following code. The code works like you enter a password called "uoc" and it shows as OK. But surprisely when you entered another password as "Cambridge" it works fine too.

I think that the problem is in the array declaration but i'm checking resources and no success!

Code:
#include <stdio.h>
#include <string.h>
struct {
char str[8];
char ok;
} data;

[Code] ......

View 3 Replies View Related

C :: Comparing Each Element Of One Array With Another

Mar 6, 2015

how to compare each element of array with another,defined ? I have tried this,but didn't work.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bill()

[Code].....

View 3 Replies View Related

C :: Comparing Pointer And Integers?

Mar 25, 2013

I am trying to make a game where you have a secret code that is coded with colors like ROYG (red,orange,yellow,green) and I am having trouble when it tells you when you have a right color in the right spot or a right color in the wrong spot when you guess a color. How can I change my code under the function int comparearray where it will compare pointers to pointers and not integers and give me the correct number of "almost" and "correct".

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 100
#define COLS 4
}

[code]....

View 4 Replies View Related

C :: Comparing Character To A Number

May 5, 2013

I created a function to check whether the first character of a string is a number or not by comparing it to every base 10 digit using a for loop. If it is, the string is valid, if it is not, the string is not valid

Code:
//ensure the first character is not a number
int ValidFirstChar(char FirstChar) {
int IsChar = TRUE, DigitCount = 0; /*boolean value indicating whether the first character is a number or not.*/

//check through all base 10 digits
for (DigitCount = 0; DigitCount <= 9; DigitCount++) {
if ((int)FirstChar == DigitCount)

[Code] ....

I have checked for the case where the first character is a number but it is displaying the error message for it. I have tried typecasting the char variable but that has not worked.

View 2 Replies View Related

C :: Comparing Two Dates Using A Structure

Jul 23, 2013

my problem is naming the function larger() with "int". At least that is what my compiler is leading me to believe.

Code:

#include <stdio.h>
struct Date{
int month;
int day;
int year;
};

[code]....

View 8 Replies View Related

C++ :: Comparing Two Floats Ranges?

Dec 24, 2014

I'm trying to compare two float ranges and it seems to be a hit and miss. I'm trying to use a object origin and dimensions comparing it to another set for collisions.

View 12 Replies View Related

C++ :: Comparing Arrays To Have Same Elements

Sep 2, 2014

If I have 2 arrays, say array1 = {2, 2, 4, 5, 6} which has sorted array elements and array2 = {4, 2, 6, 2, 5} that has same elements as array1 but not in sorted condition. How can I compare that both arrays have same elements. I have written the following code which works in all conditions except when array1 has two same elements and array2 has all different elements.

counter=0;
for (i=0; i<5; i++) {
for (int j=0; j<5; j++)
if (array2[i] == array1[j]) {
counter++;
array1[j]=0;

[Code] ....

View 7 Replies View Related

C++ :: Comparing Chars In A While Statement

Jul 1, 2014

I am trying to do this but it can never seem to work.

#include <iostream>
using namespace std;
int main () {
char charOne;
cin.get(charOne);
cin.ignore(1000, '

[Code] .....

Basically i want to keep looping until i enter an a or A (also does this apply to if statements as well?)

View 6 Replies View Related

C++ :: Comparing Two Strings With Strcmp

Jun 22, 2014

I have trouble comparing two strings with strcmp. The bold part of the code are the parts that are not working and i hope somebody can explain to me what i did wrong. Goal of the code is to compare a city name with the name of already created cities and when two of them match to creat a Street between them. Depending on the street pointer different streets can be created

my vector containing all already created cities (it seem to work)
std::vector<city*> citylist;//all cities

find city using strings and creating a road between them:

bool Map::add_street(Street *street, const string firsttown , const string secondtown) {
city* hilfs1=Map::find_city(erste);
city* hilfs2=Map::find_city( zweite);
if (hilfs1==NULL || hilfs2==NULL)
{return false ;} // Problem :both pointers are always NULL

[Code] ....

Here is also my implementation of the getname function :

std::string city::getname() {
return this->name;
}

View 2 Replies View Related

C++ :: Comparing Value To Array And Outputting

Feb 6, 2013

i have 3 arrays total, 2 of them i am comparing to see if any of the values match at all, and then i am putting the value that matches into the 3rd array. currently i am trying

int isthere (int match[], int maxmatchLength,
const int vec1[], int vec1Length,
const int vec2[], int vec2Length)
{
for (i=0; i<vec1Length; i++)
if vec1[i]==vec2[i];
vec1[i] = match[i];
}

But this will just match the same values are in the same spot. how do i fix it so that it compares one value in the first array to the whole second array, before going to the next number.

View 1 Replies View Related

C++ :: Comparing Two String In If Statement

Aug 31, 2014

i actually i want store string in r and try to compare other string (room_no) in function check but i try many time it still having error

#include<iostream>
#include<conio.h>
#include<fstream>

[Code].....

View 2 Replies View Related

C++ :: BST Comparing Parents To The Root

Mar 18, 2014

Lets say for example I have a BST that is sorted by char names, using strcmp. IF greater than 0 go right else go left.

I.E (this is just an example, they are not inserted correctly)

cat
/
dog buffalo
/ /
fish mouse zebra snake

I wanted to make a copy of this BST IF the length of the nodes are greater than the root, how would I approach this? I kinda started on this but I'm not sure if I'm making this more difficult than it should be.

void BST::copygreater(node * root, node *& dest, int & holder) {
if(!root) {
dest = NULL;
return;
}

holder = strlen(root->name) + 1; //Don't know about this? If we do a recursive call then the value would change every call?

[Code] ......

View 2 Replies View Related

C/C++ :: Comparing Strings With Pointers

May 29, 2014

I have wrote the code below that compares to strings and sees if they are equals doesn't matter the order of the words . In the question we where asked not to use any library functions like the string functions in <string.h> and we have to do that all with pointers . I debugged my code and for some reason the first loop in the function keeps looping ...

#include<stdio.h>
int IsEqual(char* str1,char* str2);
#define SIZE 20
void main() {
char str1[SIZE]="my name is monaya",str2[SIZE]="name monaya is my";
if (IsEqual(str1,str2))

[Code] ......

View 4 Replies View Related

C/C++ :: Comparing 2D Char Array?

Jun 22, 2014

I am working on a program to find uppercase, lowercase and digits in a 2D char array. When I try to use an if statement to increase the counter, I get an error "no conversion from 'int' to 'char*'". This is the if statement I am using.

if(myArray[j] <='9' || myArray[j] >='0')

View 7 Replies View Related

C/C++ :: Comparing Two Arrays For Same Elements

Feb 17, 2015

int result = 1;
for (int j=0;j<N;j++) {
bool found = false;
for (int i=0;i<N && !found;i++) {
if (a[j] == b[i]) found = true;
}
if (!found) return 0;
}

I need to create a code that compares two in arrays without sorting them. They have to be the same length and contain the same elements in any order.

every integer in a[] is also in b[]
every integer in b[] is also in a[]
all such common values appear exactly the same number of times in both a[] and b[]

EX: a = {1, 2, 3}, b = {2, 3, 4} return 0 a = {1, 2, 3}; b = {2, 3, 1} return 1 a = {1, 2, 2}; b = {2, 2, 1} return 1 a = {1, 2, 2}; b = {2, 1, 1} return 0 a = {1, 1, 2, 2, 2}; b = {2, 1, 2, 1, 2} return 1

This is all i have...

View 8 Replies View Related

C++ :: Comparing Two Data Files?

Feb 15, 2013

I'm trying to compare two data files character by character to see if they are the same. If the files are different, the program should print out the line numbers of where they do not match. So far, I have the part where it returns 'T' for when the files are the same and 'F' when they are not. But how should I start where the program prints out the line number where the characters are different? I am thinking of an array but nor sure how to start it.

View 1 Replies View Related

C++ :: Comparing Strings Against Static String?

May 20, 2014

I'm writing a code generator that produces a function from the strings to the ints. I'll be using the generated code as a "from string to enum" utility. For example:

Code: enum Color {
Red, Green, Blue, Banana
};
// The definition of colorFromString is generated somewhere.
Color colorFromString(const std::string & s);

[Code].....

The implementation of the generated code is a trie. I've seen implementations in the past (including the one at work that I'd like to replace).

Anyway, say you need to compare the region of the string

Code: const char s[] = "holiday"; from index 3 until before index 6 against the string "ida".

I can see two bits of code that my generator could produce. One is

Code: bool hasIda = std::equal(s + 3, s + 6, "ida"); and the other is
Code: bool hasIda = s[3] == 'i' && s[4] == 'd' && s[5] == 'a';

The existing code generator uses the latter method, claiming (I think) that the generated instructions are more efficient on some architectures. Is there any way to determine which is better generally, or do I have to examine the assembly produced on all target platforms?

View 4 Replies View Related

C :: Comparing Structures From Binary File?

Jul 20, 2014

I'm wondering which method faster in comparing structures from a binary file.

Method 1:
Reading through the file one structure and a time and comparing each structure.

Method 2:
Reading numerous structures and allocating to memory , then reading through each allocated memory and comparing.

View 3 Replies View Related

C :: Making Program For Comparing Strings

Oct 16, 2013

I know there is a function for comparing string but i am making it to just improve my concepts.error : 2 is coming as answer when i put same sting in both place.

Code:

#include<stdio.h>
main()
{
char str1[] = "hello" ;
char str2[]= "hello" ;
int q;
q = xstrcmp(str1,str2);
printf("%d",q);
}

[code]....

View 5 Replies View Related

C++ :: Floating Point Representation - Comparing With Zero

Nov 1, 2014

I'm currently trying to learn about floating point representation in depth, so I played around a bit. While doing so, I stumbled on some strange behaviour; I can't really work out what's happening...

#include <iostream>
#include <cmath>
using namespace std;
int main(){
float minVal = pow(2,-149); // set to smallest float possible
float nextCheck = ((float)((minVal/2.0f))); // divide by two

[Code] ....

Essentially what's happening is:
- I set minVal to be the smallest float that can be represented using single precision
- Dividing by 2 should yield 0 -- we're at the minimum
- Indeed, isZero2 does return true, but isZero returns false.

What's going on -- I would have thought them to be identical? Is the compiler trying to be clever, saying that dividing any number cannot possibly yield zero?

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved