Hi,
First a preface: I am 100% new to c++. my only "coding" experience is matlab, and excel.
my assignment is to create a c++ program that asks for a number of resistors in series, and sums up the total resistance. The code is also supposed to display an array with all of the inputted resistance values. it will always be a 1 row, x column array, where x is the number of resistors
Here is the code i have for the actual resistance calculation
#include<iostream>
usingnamespace std;
int main ()
{
int sum = 0;
int previous = 0;
int number;
int numberitems;
int count = 0;
cout << "Enter the number of Resistors in series: ";
cin >> numberitems;
do
{
cout << "Enter the resistor value : ";
cin >> number;
sum+=number;
count++;
previous = sum;
}
while ( count < numberitems);
cout<<"Your total Resistance is: "<< sum<< " ohms"<< endl;
return 0;
}
Now, I have very little understanding of how c++ works, it is a continuing process for me. Any help is very much appreaciated.
I tried using some matrix code, but I am not sure if I should go vector route or what. This is all new to me.
Thank you!
Edit: The code I got earlier from working with a few classmates. I understand how that works, but the matrix part is what gets me.
to store values you could use std::vector, you just have to #include <vector>
1. You create it and you dont have to spcify its size just create it like vector<int> ressitances; // empty vector containing no elements
It will resize itself as you continue to add elements thats one of the resons why its easier to use than an array
2. Than you can add elements to it within you while loop using push_back() member function like this resistances.push_back(number);
1 2 3 4 5 6
vector<int> vec;
vec.push_back(1); //elements in vec : 1
vec.push_back(4); //elements in vec : 1, 4
vec.push_back(22); //elements in vec : 1, 4, 22
vec.push_back(20); //elements in vec : 1, 4, 22, 20
3. After you added elements you can display them like this
1 2 3 4 5 6
cout << "Resistances entered : { ";
for(unsignedint i = 0; i < resistances.size(); ++i){ //subscripting will start from 0. Its the 1st elem
cout << resistances[i]; //use subscripting to access individual elements
if(i+1 < resistances.size()) cout << ", "; //check if there will be next element
}
cout << " }\n";
The goal is to display the total resistance (sum of all the inputs) and an array showing those resistances,
so if i say there are 4 resistors, and they have values of 1, 36, 2, 18 ohms, it would show "total resistance is: 57" and an array with <1 36 2 18> to show the resistors.
#include<iostream>
usingnamespace std;
int main(){
int quantity;
cout<<"Enter the number of the resistors:\n";
cin>>quantity;
int transistors[quantity];
int sum = 0;
int value;
cout<<"Enter the value of the resistance for each resistor\n";
for (int i=0; i<quantity; i++){
cin>>value;
sum +=value;
transistors[i]=value;
}
cout<<"Total resistance is "<<sum <<" ohms\n";
cout<<"You entered the following resistances: {";
for (int i=0; i<quantity; i++){
cout<<transistors[i]<<", ";
}
cout<<"}";
return 0;
}
int quantity;
cout<<"Enter the number of the resistors:\n";
cin>>quantity;
int transistors[quantity];
Unfortunately this code doesn't compile:
[Error] ISO C++ forbids variable length array 'transistors' [-Wvla]
In standard C++ the size of an array must be a constant known at compile time. The above code depends on some non-standard extension.
That's why the use of std::vector was suggested.
The line int transistors[quantity]; can be replaced with std::vector<int> transistors(quantity);
Another alternative is to declare a fixed-size array larger than required, say 100 elements, and make sure that the user requests a quantity no larger than this.
I know that it isn't standard C++. But from my understanding, the compiler they use at their course can use a non constant as size. If not it can be done the following way, as Chervil said (even though it might use more memory than a vector)
#include<iostream>
usingnamespace std;
int transistors[10000];
int main(){
int quantity;
cout<<"Enter the number of the resistors:\n";
cin>>quantity;
int sum = 0;
int value;
cout<<"Enter the value of the resistance for each resistor\n";
for (int i=0; i<quantity; i++){
cin>>value;
sum +=value;
transistors[i]=value;
}
cout<<"Total resistance is "<<sum <<" ohms\n";
cout<<"You entered the following resistances: {";
for (int i=0; i<quantity; i++){
cout<<transistors[i]<<", ";
}
cout<<"}";
return 0;
}
He is a beginner in C++, I don't think he has reached vectors yet.
I've no inside knowledge about which course it is, or what is or isn't permitted. It's just a good idea when starting out to be aware of what are the standard ways of doing things. The idea being, it's much easier to learn how to do things things the right way from the start than it is to unlearn at a later date.
#include<iostream>
#include <vector>
usingnamespace std;
int main ()
{
int sum = 0;
int number;
int numberitems;
int count = 0;
vector<int> resistances; // step 1 create empty vector
cout << "Enter the number of Resistors in series: ";
cin >> numberitems;
do
{
cout << "Enter the resistor value : ";
cin >> number;
resistances.push_back(number); //step 2 put elements in it every time loop executes
sum+=number;
count++;
}
while ( count < numberitems);
cout << "Resistances entered : { "; //step 3 display vector elements
for(unsignedint i = 0; i < resistances.size(); ++i){
cout << resistances[i];
if(i+1 < resistances.size()) cout << ", ";
}
cout << " }\n";
cout<<"Your total Resistance is: "<< sum<< " ohms"<< endl;
return 0;
}
@jgg2002 your code original code worked perfectly, and created what I needed.
Only yesterday I found out that I needed to use a loop (I think they mean push_back), and I kind of understand how that works. They said what I handed in will suffice though.
We haven't reached vectors yet in terms of C++, but in matlab and excel it's no issue. We use C++ in electrical engineering to quickly and efficiently solve circuits, write algorithms and route pcb's, and electronics programming as well. Eventually I will get the hang of it, and my professor does have some info on C++. I don't think I am required to take a course in it though.
For the issues in compilation - I am using the program DevC++, so maybe it has some backend that others don't?