Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [Help] Basic C++ assignment!
New Posts  All Forums:Forum Nav:

[Help] Basic C++ assignment!

post #1 of 21
Thread Starter 
Part of this assignment I am working on is to enter the number of resistors in a circuit and then ask the user to enter the value of each resistor and lastly add them all up.

I can usually figure my way around by fiddling but, this time, I don't even know where to start! Help will be greatly appreciated/rewarded!


EDIT: Read last post first!
Edited by MeeMoo220 - 3/4/11 at 4:47am
post #2 of 21
I'm guessing you're in the section learning about arrays.

You'll need an array for resistors.
In your input function/loop, you'll need an input for each array index up to the specified amount of array members.

Lastly in your 'computational' function, you will need to retrieve the value of each array member and add it to a variable value.
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
post #3 of 21
Thread Starter 
Quote:
Originally Posted by beers View Post
I'm guessing you're in the section learning about arrays.

You'll need an array for resistors.
In your input function/loop, you'll need an input for each array index up to the specified amount of array members.

Lastly in your 'computational' function, you will need to retrieve the value of each array member and add it to a variable value.
Here's what the assignment has to say about Arrays:

Quote:
Because we have not yet learned about arrays, and because you don’t know ahead of time how many resistors the user is going to enter data for, you will not be able to store the resistances of the individual resistors. This has the following consequences:

 You will have to calculate the summation term of the equivalent resistance as you read in the resistances, updating the sum after each resistance is read in.

 You will not be able to echo back the individual resistances to the user. You still can and should, however, echo back all of the rest of the user’s input.
I have no idea what that means. If it makes sense to u, please explain it to me. Also, thanks for the swift reply!
post #4 of 21
numResistors = user input
define array[numResistors]

for(i=0; i<numResistors; i++){
array[i] = resistor value
}

totalResistance = blahblah


Huge hangover and I'm not going to write it for you in proper C++, so that'll have to do.
Folder #1
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II X4 620 MSI K9A2 Platinum 4x9600GSO 4x1GB Kingston DDR2-800 
Power
XFX 650W 
  hide details  
Reply
Folder #1
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II X4 620 MSI K9A2 Platinum 4x9600GSO 4x1GB Kingston DDR2-800 
Power
XFX 650W 
  hide details  
Reply
post #5 of 21
Woops double post
Folder #1
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II X4 620 MSI K9A2 Platinum 4x9600GSO 4x1GB Kingston DDR2-800 
Power
XFX 650W 
  hide details  
Reply
Folder #1
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II X4 620 MSI K9A2 Platinum 4x9600GSO 4x1GB Kingston DDR2-800 
Power
XFX 650W 
  hide details  
Reply
post #6 of 21
Thread Starter 
Lemme put that in a separate code and try to learn arrays through it. Thanks a lot for the input. BRB, trying it out!
post #7 of 21
Without arrays, the requirement you've listed is pretty simple.

Use the input for 'number of resistors' as your loop reference integer.
Use a single value to store the sum of resistor inputs, and simply add the new value on top of the existing value each time you prompt for one.

Once the loop is finished, output the sum value.
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
post #8 of 21
Thread Starter 
Quote:
Originally Posted by beers View Post
Without arrays, the requirement you've listed is pretty simple.

Use the input for 'number of resistors' as your loop reference integer.
Use a single value to store the sum of array values, and simply add the new value on top of the existing value.

Once the loop is finished, output the sum value.
Soo something like..



cin>>ResNum;

{

ResValue+= *something here*;

}

cout<<ResValue;


Wait, what did u mean by loop reference integer? I don't understand that part. Please bear with me here.
post #9 of 21
Hey no worries.
Something to the effect of:

int loopnum = 0;
int loopcount = 0;
double userinput = 0;
double total = 0;
cout << "Please enter the amount of resistors : ";
cin >> loopnum;
while (loopcount < loopnum){
cout << "Resistor " << loopcount +1 << " value : ";
cin >> userinput;
total = total + userinput;
++loopcount;
}
cout << "Total Value : " << total;

Haven't used C++ since about high school so sorry for any glaring syntax errors
Edited by beers - 3/4/11 at 3:19am
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
post #10 of 21
Thread Starter 
Quote:
Originally Posted by beers View Post
Hey no worries.
Something to the effect of:

int loopnum = 0;
int loopcount = 0;
double userinput = 0;
double total = 0;
cout << "Please enter the amount of resistors : ";
cin >> loopnum;
while (loopcount < loopnum){
cout << "Resistor " << loopcount +1 << " value : ";
cin >> userinput;
total = total + userinput;
++loopcount;
}

Haven't used C++ since about high school so sorry for any glaring syntax errors
Holy crap! that worked, however, if I input 3 resistors as the number, it asks me starting at resistor 0. so I end up having to enter resistor 0,1 , and 2.

I'm guessing it has something to do with the loop index having to start at 1 and not 0?



EDIT: made loopcount =1 and it worked.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [Help] Basic C++ assignment!