Solved

setting two double variables equal to the larger of the two

Posted on 2009-04-04
10
305 Views
Last Modified: 2012-05-06
I'm trying to make a program that initializes two numbers and states what they are. And then runs a function that sets both numbers equal to the larger of the two. And then it restates what the numbers are. The result I'm getting is this, which is wrong:

The first number is 0 and the second number is 1075052544
The first number is 0 and the second number is 1075052544

How can I get it to say this:

The first number is 5 and the second number is 10
Now, the first number is 10 and the second number is 10

#include <stdio.h>

void larger_of(double * x, double * y);

int main () 

{	

	double a = 5;

	double b = 10;

	printf("The first number is %d and the second number is %d\n", a, b);

	larger_of(&a,&b);

	printf("Now, the first number is %d and the second number is %d\n", a, b);

	return 0;

}
 

void larger_of(double * x, double * y)

{

	if (x > y)

		*y = *x;

	if (y > x)

		*x = *y;

	

}

Open in new window

0
Comment
Question by:prebek
  • 4
  • 2
10 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 24069687
For double's, you use %f, not %d (which is for ints).

Maybe you should have a look at the reference pages for printf and scanf :

        http://cplusplus.com/reference/clibrary/cstdio/printf.html
        http://cplusplus.com/reference/clibrary/cstdio/scanf.html

Take special note of the formats explained there.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 500 total points
ID: 24069693
Also, in your larger_of function, you have this comparison :

>>         if (x > y)

which won't work as you expect, since x and y are pointers-to-double. They're not the double values themselves, so you're basically comparing their memory addresses, rather than their values.

And this :

>>                 *y = *x;

will copy the double value pointed to by x to the double value pointed to by y. So, both doubles will have the same value after this. Was this intentional, or did you want to swap the values ?
0
 

Author Comment

by:prebek
ID: 24069908
Infinity, I realize that I'm bad at communicating values from a short function to the main function, and obviously you know how to do it. Could you tell me the exact code I would have to use in order to turn the first number and second number into the larger of the two numbers, to get my program to say this:


The first number is 5 and the second number is 10
Now, the first number is 10 and the second number is 10
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 53

Expert Comment

by:Infinity08
ID: 24070486
amvasquez01, you repeated exactly what I said, with the only addition being the full code - and that's in fact not allowed on this site - we're not allowed to post full-code solutions to assignments. Instead, we guide the asker to find the solution himself.

prebek, I know you already accepted his post, but please ignore it, and try to solve it yourself. It will be a better learning experience for you.

For the same reason as mentioned elsewhere, I'm gonna ask to get this question re-opened.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24178918
My suggestion :

http:#24069687 (Infinity08) : first issue explained
http:#24069693 (Infinity08) : second issue explained
0
 

Author Closing Comment

by:prebek
ID: 31566680
Thank you so much, it's hard to get an answer that shows the correct code rather than explains it :)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is string constant address ? 10 192
Passing a array as parameter - C 2 84
Problem to save 10 148
What is sub-make ? 2 39
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now