• 0

C Programming help


Question

The program is to;

 

  1. Declare the variables R1, Rf, V1 and Vout as data types of your choice.

 

  1. Allow the user to enter the value for V1.

 

  1. Calculate the value of Vout

(what happens if Vout exceeds the supply voltage?)

 

  1. Display all of the voltage values to the screen using appropriate formatting
int main()
{
//declaring variables:
float r1, rf, v1, vout;
r1=10000
rf=10000
}
printf("allow the user to enter the value for v1");
scanf("%f",&v1);
vout= (rf/r1)*v1;

First, I have declare the four variables as float and initialize them. Then I allow the user to enter the value of v1 of thier choice. Now I am trying to run the programme and it won't and gives me warning of unused variables (v1, vf). I am kinda stuck here and don't know what to do next. Any sort of help would be appreciated. I have attached the whole question please do have a look.

post-522745-0-57408600-1405261445.png

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Well the first obvious problems:

  • You've placed the printf and scanf functions, and the vout assignment outside of main. You can't do that. All code must exist inside a function (i.e. main()).
  • You're missing a semicolons at the end of these two lines:

    r1=10000   //< No semicolon
    rf=10000    //< Semicolon missing here too.
Once you've fixed those issues, let us know if you need anymore help.
Link to comment
Share on other sites

  • 0

Is this an assignment? Please read this first. You need to show what you've tried yourself first and tell us what specific issue you're having.

I have already showed my working. And it is not an assignment. I have been trying to use c programming to solve electrical engineering problems. I have never used c programming before but I do went through some tutorials online and tried to solve this problem myself.

Link to comment
Share on other sites

  • 0
#include <stdlib.h>
#include <stdio.h>

static const int R1 = 10000;
static const int RF = 10000;

int
main ( void )
{
float v1, vout;

puts ( "Please enter input voltage [V1]" );
scanf ( "%f", &v1 );

vout = ( RF / R1 ) * v1;
if ( vout > v1 )
puts ( "Output voltage exceeds input voltage!" );

printf ( "R1: %i, RF: %i, v1: %f, vout: %f\n", R1, RF, v1, vout );

return EXIT_SUCCESS;
}

gcc -pedantic -ansi -o v1 v1.c

Link to comment
Share on other sites

  • 0

@ simplezz

 

 

what happens if Vout exceeds the supply voltage? and What does it mean by display all of the voltage values to the screen using appropriate formatting?

Link to comment
Share on other sites

  • 0

@ Majesticmerc

I did managed to get it to debug run mode but it's not calculating the vout.

I have attached a photo do see.

You're missing a newline in printf. See my post for example. And you're calculating vout before the input.

Link to comment
Share on other sites

  • 0

@ simplezz

what happens if Vout exceeds the supply voltage?

No idea. I'm not an engineer. Is it even possible in a real circuit with resistance? The code I posted just tested for the condition that's all.

and What does it mean by display all of the voltage values to the screen using appropriate formatting?

See my example. Specifically the printf at the end. It's just asking you to print the resultant values basically.
Link to comment
Share on other sites

  • 0

@ Majesticmerc

I did managed to get it to debug run mode but it's not calculating the vout.

I have attached a photo do see.

@OP, take another look at what you've written here to help you understand what you did wrong. Walk through the code in main()line by line.

- You're declaring some variables

- You're setting the values in two of them. All food so far.

- Now you're doing a calculation and storing the result in one of the variables. But wait, in the calculation you're using a variable which you have not yet assigned a value to. Also, isn't the calxulation supposed to be done AFTER getting the Vin value from the user... Here you're doing it before!

- Next you're outputting some text to the command prompt your program is running within. It would be much better if you replace the text you'rr outputting with something more on the lines of "Please provide a value for Vin: ".

- Then you're capturing the user's input and storing it in your variable.

- Now is when you should do the calculation!

- And finally, you're using getchar() for some random reason and not bothering to store the value it fetches anywhere.

You've already been given a much more complete solution that you had, and I'm struggling to type a useful response here on my phone, so I think I'll stop with this at this point, and maybe come back later when I'm at my PC.

One thing I'll leave you with though, so far none of the code I've seen actually does the calculation correctly; the equation in the diagram has a minus sign in it that seems to have been overlooked!

Link to comment
Share on other sites

  • 0

I have already showed my working. And it is not an assignment. I have been trying to use c programming to solve electrical engineering problems. I have never used c programming before but I do went through some tutorials online and tried to solve this problem myself.

 

Vick472 I would highly recommend you put more time into learning the C programming language first as many of the questions or mistakes can be avoided along with helping you understand how to solve your engineering problems easier.  This will be especially important if you want to increase your knowledge of electrical engineering to the expert level.  Also brush up on your electronics as if you overload things you can cause other things to stop working or cause a fire.

 

I would recommend you pick up the following books in this order:

"C for Programmers with an Introduction to C11"

"The C Programming Language 2nd Edition",

"Expert C Programming"

"A C Reference Manual (5th Edition)" "The C Standard Library".

 

To get you up to speed on Electronics I would recommend the following:

"Electrical Engineering 101, Third Edition: Everything You Should Have Learned in School...but Probably Didn't"

"Electrical Engineering: Principles and Applications (5th Edition)"

"Practical Electronics for Inventors"

 

Along with if you do not already have the following toolkit:

Regular Electronics Breadboard

Arduino

RaspberryPi

Nice electrical multimeter clamps

Good quality electrical tape

High quality electricians gloves

High quality solder and soldering iron

Link to comment
Share on other sites

  • 0

I have already showed my working. And it is not an assignment. I have been trying to use c programming to solve electrical engineering problems. I have never used c programming before but I do went through some tutorials online and tried to solve this problem myself.

 

HAHAahahahah, that's so clearly an assignment question from layout and phraseology.

post-522745-0-57408600-1405261445.png

Link to comment
Share on other sites

  • 0

- And finally, you're using getchar() for some random reason and not bothering to store the value it fetches anywhere.

He's probably doing that to keep the console window open so he can see the output. Unfortunately, it automatically closes after running a set of commands unless it's manually opened and the program is run from it.

I always write / run my code in a Linux terminal anyway, so it's not necessary.

 

One thing I'll leave you with though, so far none of the code I've seen actually does the calculation correctly; the equation in the diagram has a minus sign in it that seems to have been overlooked!

Is that a minus? Guess I overlooked it too. Well spotted.

So does that make it Vout = -1 * Vin using those constants?

Edit: I just realised I was confusing voltage with current when I said it couldn't be more than the input.

Link to comment
Share on other sites

  • 0

He's probably doing that to keep the console window open so he can see the output. Unfortunately, it automatically closes after running a set of commands unless it's manually opened and the program is run from it.

I always write / run my code in a Linux terminal anyway, so it's not necessary.

 

Maybe, maybe, but it's just picking up the newline (ascii 0xA) character, so that's not going to work. @OP, if you need to do that, i.e. get your program to pause at the end so you can see what's going on in the terminal/command-line-window before it closes, which it will unless you launch the program you've compiled from within an already open terminal/cmd-prompt, (or unless your IDE happens to automatically make it pause when you run it from within your IDE), you can temporarily add in the command:

system("pause");

It's a bit of a hack itself, don;t leave it in your "production" code, but it's much cleaner and easier for other's reading your code to understand your intentions, never mind that it'll actually do the job you're asking of it. Alternatively, if running from within your IDE another and better option would be to simply add a breakpoint on the return statement.

 

Better go eat my dinner...

Link to comment
Share on other sites

  • 0

@OP, if you need to do that, i.e. get your program to pause at the end so you can see what's going on in the terminal/command-line-window before it closes, which it will unless you launch the program you've compiled from within an already open terminal/cmd-prompt, (or unless your IDE happens to automatically make it pause when you run it from within your IDE), you can temporarily add in the command:

system("pause");
It's a bit of a hack itself, don;t leave it in your "production" code, but it's much cleaner and easier for other's reading your code to understand your intentions, never mind that it'll actually do the job you're asking of it. Alternatively, if running from within your IDE another and better option would be to simply add a breakpoint on the return statement.
Yeah I think the breakpoint method or just manually opening a terminal/console is a better solution, if that was indeed his intention. Calling out to the shell (pause) is platform specific and very bad practise, even worse than getchar() in my opinion.
Link to comment
Share on other sites

  • 0

Yeah I think the breakpoint method or just manually opening a terminal/console is a better solution, if that was indeed his intention. Calling out to the shell (pause) is platform specific and very bad practise, even worse than getchar() in my opinion.

 

Of course, I'm with you on that, but it can sometimes be useful to a new programmer.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.