Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple C assignment

Posted on 2012-03-13
3
Medium Priority
?
361 Views
Last Modified: 2012-03-13
I'm working on a simple assignment, in C. I'm having trouble getting the variables to be calculated. My output just repeats a small number and doesn't get anywhere near the value I need. This is my code that I'm utilizing:

#include <stdio.h>
#include <math.h>
#include <iostream>

int main () {

      double current, resistance, watts, voltage, miles, power_loss, total_resistance;
      
      watts = 5000000000000000000;
      voltage = 100;
      resistance = 0.05;
      miles = 20;
      current = 0;
      power_loss = 0;

      printf("Distance      Power Loss\n");

      for(miles = 20; miles <= 100; miles += 10)
      {
            total_resistance = resistance * miles;
            current = watts / voltage;
            power_loss = (current * current) / total_resistance;
            
            printf("%g      %g\n", &miles, &power_loss);
      }

      system ("pause");
      return 0;
}

This is what my output looks like:

Distance                  Power Loss
1.3477777e-308     0.000000000
1.3477777e-308     0.000000000
1.3477777e-308     0.000000000
1.3477777e-308     0.000000000
1.3477777e-308     0.000000000
1.3477777e-308     0.000000000
1.3477777e-308     0.000000000
1.3477777e-308     0.000000000
1.3477777e-308     0.000000000


Can anyone help?
0
Comment
Question by:gabos
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
rowansmith earned 2000 total points
ID: 37713895
OK, so you are printing the "address" of miles and power_loss.  Not the values that are contained at those addresses.

You simply need to drop the &.
0
 
LVL 11

Expert Comment

by:rowansmith
ID: 37713923
I would suggest reading, (and of course understanding) this programming lesson.  I spent programming hours trying to learn these concepts :-)

Lesson 6: Pointers in C++
0
 

Author Closing Comment

by:gabos
ID: 37714087
That was the issue. I misunderstood the ampersand symbol. It is so much clearer now. Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Make the most of your online learning experience.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

971 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