The program should read n resistances and a voltage from the keyboard and then calculate the equivalent resistance and the current.
My problem is that it calculates based only on the last entered resistance.
Is it possible to declare a method inside a function? or should I give up this completely unpractical approach
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
class rez {
float r;
public:
void set(int n);
float val() { return r; }
};
void rez :: set(int n) { //n is the number of resistances
int i;
for (i = 1; i <= n; i++) {
cout << "R" << i << "=";
cin >> r;
}
}
float serie(rez r1,int n)
{
float s=0;
int i;
for (i = 1; i <= n; i++)
{
s = s+ r1.val();
}
return s;
}
float para(rez r1, int n)
{
float s = 0;
int i;
for (i = 1; i <= n; i++)
{
s = s + (1/r1.val());
}
return 1/s;
}
int main()
{
char c, k = 'y'; // 'c' selects series or para
rez r1;
int n;
cout << "number of resis:";
cin >> n;
cout << endl;
while (k != 'q')
{
r1.set(n);
float i, u;
cout << "\n Vdc= ";
cin >> u;
cout << endl;
cout << "series or para(s/p)?"<<endl;
cin >> c;
switch (c)
{
case('s'):cout <<"\n equiv resistance = "<< serie(r1,n)<<endl;
i = u / serie(r1, n);
cout << "curr i = " << i << " amp";
break;
case('p'):cout << "\n equiv res = " << para(r1, n)<<endl;
i = u / para(r1, n);
cout << "cur i = " << i << " amp";
break;
}
cout <<endl<< "\n another set?(y/q)?"<<endl;
cin >> k;
}
return 0;
}
It is because when you read in the resistances you are setting the value of the total resistance each time not adding to the total resistance.
void rez :: set(int n) { //n is the number of resistances
int i;
for (i = 1; i <= n; i++) {
cout << "R" << i << "=";
cin >> r; // <- this sets the value of r, it does not add to it
}
}
To fix this you should create a temporary variable to store the input resistance and then add it to the total resistance
void rez :: set(int n)
{
int i;
for (i = 1; i <= n; i++)
{
float input;
cout << "R" << i << "=";
cin >> input;
r += input;
}
}
Related
I want the program to ask the user to enter a number and then that function in the array will display on the screen
e.g
case 0 = displayNums (displays numbers entered by the user)
case 2 = getAverage (gets average of numbers entered)
I tried to code the menu to do that, but it only shows up. Nothing happens when the number for the specific function is entered.
#include <iostream>
#define integer 12
int ShowMenu(void);
double displayNums(double[], int);
double GetTotal(double[], int);
double getAverage(double[], int);
double getLargest(double[], int, int*);
double getSmallest(double[], int, int*);
int getNumOccurence(double[], int, int n);
double scaleUp(double[], int);
using namespace std;
int main() {
cout << "enter 12 integers 1 by 1:\n";
int data;
int n = 1;
// array to hold the integers
double arr[integer];
// get the integers
for (int i = 0; i < 12; i++) {
cin >> arr[i];
}
int option;
do {
option = ShowMenu();
switch (option) {
case 0:
double displayNums();
break;
case 1:
double GetTotal();
break;
case 2:
double getAverage();
break;
case 3:
double getLargest();
break;
case 4:
double getSmallest();
break;
case 5:
int getNumOccurence();
break;
case 6:
double scaleUp();
break;
case 7:
break;
default:
cout << "invalid option\n";
}
} while (option != 7);
// displays numbers entered by user
cout << displayNums(arr, integer) << endl;
// displays sum of numbers entered
cout << GetTotal(arr, integer) << endl;
// displays the average
cout << "Average integer is:" << getAverage(arr, integer) << endl;
// displays the largest
cout << "Largest integer is: " << getLargest(arr, integer, &data) << endl;
// display the smallest integer
cout << "Smallest integer is:" << getSmallest(arr, integer, &data) << endl;
// display the occurence of the num
cout << "Occurence integer is:" << getNumOccurence(arr, integer, n) << endl;
// display the scale up integers
cout << "Scaled up integers are:" << scaleUp(arr, integer) << endl;
return 0;
}
int ShowMenu(void) {
int option;
cout << "\t0. Display Numbers\n";
cout << "\t1. Get Total of numbers\n";
cout << "\t2. Get Average\n";
cout << "\t3. Get Largest\n";
cout << "\t4. Get Smallest\n";
cout << "\t5. Get Number Occurences\n";
cout << "\t6. Scale Up\n";
cout << "\t7. Quit\n";
cout << "\t\t\tOption ? ";
cin >> option;
return option;
}
double displayNums(double arr[], int size) {
double display = 0;
for (int i = 0; i < size; i++) { // for loop
}
cout << "the numbers that you have entered into the array are:" << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << endl; // displays numbers entered
}
return display;
}
double GetTotal(double arr[], int size) {
double sum = 0;
for (int i = 0; i < size; i++) {
}
cout << "" << endl;
for (int i = 0; i < 12; i++) {
sum += arr[i];
}
cout << "The sum of all numbers entered is:" << sum << endl;
return sum;
}
double getAverage(double arr[], int size) {
double sum = 0.0, avg;
for (int i = 0; i < size; i++) {
sum = sum + arr[i];
}
avg = sum / size;
return avg;
}
double getLargest(double arr[], int size, int* data) {
double large = 0;
for (int i = 0; i < size; i++) {
if (arr[i] > large) {
large = arr[i];
*data = i + 1;
}
}
return large;
}
double getSmallest(double arr[], int size, int* data) {
double small = 10000;
for (int i = 0; i < size; i++) {
if (arr[i] < small) {
small = arr[i];
*data = i + 1;
}
}
return small;
}
int getNumOccurence(double arr[], int size, int n) {
// only works when you insert repetable number 1
int count = 0;
for (int i = 0; i < size; i++) {
if (n == arr[i]) {
count++;
}
}
return count;
}
double scaleUp(double arr[], int size) {
int factor = 0;
cout << "enter the scale up factor";
cin >> factor;
for (int i = 0; i < size; i++) {
arr[i] *= factor;
cout << arr[i] << endl;
}
return factor;
}
You could use a std::map instead of all those cases.
// Declare a synonym for a function pointer
typedef double (*Function_Pointer)();
// Declare an abbreviation for the map:
typedef std::map<int, Function_Pointer> Function_Display_Table;
// Initialize the function table:
Function_Display_Table display_table;
display_table[0] = display_nums;
display_table[1] = GetTotal;
display_table[2] = getAverage();
//...
// To process your selection:
Function_Pointer fp = display_table.at(selection);
double return_value = fp();
To expand your menu selection, you only have to add rows to the display_table.
I have a class ComplexesSet which represent a set of complexes numbers. I have to use this class to write a program which reads the complex numbers from keyboard and create a menu like that: press 1 to add a number in the set, press 2 to delete a number from set and press 0 to exit the program.
This is my program:
HEADER FILE
#pragma once
#include <iostream>
using namespace std;
#define DIMMAX 10;
class Complex {
int re, im;
public:
Complex() {
re = im = 0;
}
Complex(int re, int im) {
this->re = re;
this->im = im;
}
void display() {
cout << " (" << re << ", " << im << " ) ";
}
int equal(Complex c2); // check the equality between 2 complex numbers.
void read(); // reads a complex number
};
class ComplexesSet {
Complex* v; // the complex numbera array
int dim; // the maximum dimension of the array
int n; // the current number of complex numbers in the set
public:
ComplexesSet();
ComplexesSet(int d);
~ComplexesSet();
void addNumber(Complex); // add a number in set
void deleteNumber(Complex); // delete a number from set
void displaySet(); // display the set
};
METHODS FILE
#include "multime.h"
#include <iostream>
using namespace std;
int Complex::equal(Complex c2) {
if (this->re == c2.re && this->im == c2.im) {
return 1;
}
else {
return 0;
}
}
void Complex::read() {
cout << "Enter the real part: ";
cin >> this->re;
cout << "Enter the imaginary part: ";
cin >> this->im;
cout << endl;
}
ComplexesSet::ComplexesSet() {
cout << "Set of complexes numbers: ";
dim = DIMMAX;
v = new Complex[dim];
n = 0;
}
ComplexesSet::ComplexesSet(int d) {
cout << "ComplexesSet(" << d << ")";
dim = d;
v = new Complex[dim];
n = 0;
}
ComplexesSet::~ComplexesSet() {
cout << "~ComplexesSet" << endl;
if (v) {
delete[] v;
}
v = nullptr;
dim = -1;
n = -1;
}
void ComplexesSet::addNumber(Complex num) {
int ok = 0;
if (n < dim) {
for (int i = 0; i < n; i++) {
if (v[i].equal(num)) {
ok = 1;
i = n + 1;
}
}
if (ok) {
cout << "This element is already in the set.";
}
else {
v[n] = num;
n++;
}
}
else {
cout << "The set is full.";
}
}
void ComplexesSet::deleteNumber(Complex num) {
int i;
if (n != 0) {
for (i = 0; i < n; i++) {
if (v[i].equal(num)) {
break;
}
else {
cout << "This element does not exists.";
}
}
if (i < n) {
n = n - 1;
for (int j = i; j < n; j++) {
v[j] = v[j + 1];
}
}
}
else {
cout << "The set is empty.";
}
}
void ComplexesSet::displaySet() {
cout << "\n The set: {";
if (n) {
for (int i = 0; i < n; i++) {
v[i].display();
}
}
cout << "}.\n\n";
}
MAIN FILE:
#include "multime.h"
#include <iostream>
using namespace std;
int main() {
ComplexesSet m;
Complex num;
int option;
int x;
do {
cout << endl << "1 - ADD A NUMBER\n 2 - DELETE A NUMBER\n 0 - EXIT THE PROGRAM";
cin >> option;
switch (option) {
case 1:
cout << endl << "Enter the number you want to add: ";
num.read();
//? How to call addNumber method from ComplexesSet class?
// m.displaySet ?;
break;
case 2:
cout << endl << "Enter the number you want to delete: ";
num.read();
//? How to call deleteNumber method from ComplexesSet class?
// m.displaySet ?;
break;
}
} while (option >= 1 && option <= 2);
system("pause");
return 0;
}
I got stuck in the main file. How to call the addNumber and deleteNumber methods in order to add and delete a complex number from the set? Also, after pressing "1" or "2", after I read the number, I need to display the set.
in my project there are 2 structures, one of which relates to a binary tree, the second to students data
in ' int main ' I can’t access the add function GETDATA
' 'ZKR' does not refer to a value . ' (xcode)
I also can’t create a counter of the number of vertices of my tree
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
struct ZKR {
char FACULTY[30];
int ACADEMIC_DEGREE;
char FIO[30];
};
struct point
{
char *data;
point *left;
point *right;
};
point* tree(int n, point* p)
{
point *r;
int nl, nr;
if (n == 0) { p = NULL; return p; }
nl = n / 2;
nr = n - nl - 1;
r = new point;
char s[50];
cout << "Значение: ";
cin >> s;
r->data = new char[strlen(s) + 1];
strcpy(r->data,s);
r->left = tree(nl, r->left);
r->right = tree(nr, r->right);
p = r;
return p;
}
void GETDATA(ZKR*M, int N)
{
cin.ignore();
for (int i = 0; i < N; i++)
{
cout << "\n";
cout << "FACULTY: ";
cin.getline(M[i].FACULTY, 30);
cout << "\n";
cout << "FIO: ";
cin.getline(M[i].FIO, 30);
cout << "\n";
cout << "ACADEMIC DEGREE: ";
cin >> M[i].ACADEMIC_DEGREE;
cin.ignore();
}
}
void treeprint(point *p, int &count) {
if (p != NULL) {
treeprint(p->left, count);
cout << p->data << " ";
treeprint(p->right, count);
if ((p->left == NULL) && (p->right == NULL))
count = count + 1;
}
}
int main()
{
setlocale(LC_ALL, "russian");
srand(time(NULL));
int n = 0, k = 0, count = 0;
point *beg = nullptr;
cout << "Enter the number of students" << endl;
int N;
cin >> N;
ZKR*M = new ZKR[N];
do
{
cout << "1. BUILD a binary tree\n";
cout << "2. SHOW a binary tree\n";
cout << "3. GETDATA\n";
cin >> k;
switch (k)
{
case 1:
cout << "Введите количество элементов" << endl;
cin >> n;
beg = tree(n, beg);
cout << endl;
break;
case 2:
treeprint(beg, count);
cout << endl;
cout << "Листьев в дереве: " << count << endl;
break;
case 3:
GETDATA(ZKR*M, N);
break;
}
} while (k != 4);
system("pause");
return 0;
}
I will be grateful for any help
This line is not syntactically correct:
GETDATA(ZKR*M, N);
Replace it with:
GETDATA(M, N);
M is a pointer to an object of type ZKR.
I have a simple program that is calculating factorials, permutations and combinations. I feel good about my math but for whatever reason I cannot get this program to execute. Full disclosure I am new student to C++. Here is my code:
#include <iostream>
using namespace std;
int factorial(int);
int permutations(int, int);
int combinations (int ,int);
void perms_and_combs(int, int, int&, int&);
int numPerms;
int numCombs;
int main() {
int factorialVal;
cout << "enter an int!\n";
cin >> factorialVal;
cout << "The factorial of " << factorialVal << " is " << factorial(factorialVal) << endl;
int permVal1;
int permVal2;
do {
cout << "Input a two values: ";
cin >> permVal1;
cout << ", ";
cin >> permVal2;
} while ( permVal1 < 0 || permVal2 > permVal1);
cout << "test"; // This line doesn't get executed
perms_and_combs(permVal1, permVal2, numPerms, numCombs);
cout << "Number of permutations: "<<numPerms << ". Number of combinations: " << numCombs;
return 0;
}
int factorial(int n) {
int product = 1;
for (int i = 1; i <= n; i++) {
product *= i;
}
return product;
}
int permutations (int n, int k) {
int result;
int denominator = n-k;
cout << denominator;
result = (factorial(n)/factorial(denominator));
return result;
}
int combinations (int n, int k) {
int result;
result = permutations(n, k) * (1/factorial(k));
return result;
}
void perms_and_combs(int n, int k, int& numPerms, int& numCombs) {
numPerms = permutations(n, k);
numCombs = combinations(n, k);
return;
}
int main()
{
cout << "The maximum number of elements in an array is 100. " << endl;
const int MAX = 100;
double myarray[MAX];
int arraysize = Fill_Array(myarray, MAX);
Show_Array(myarray, arraysize);
Reverse_Array(myarray, arraysize);
Show_Array(myarray, arraysize);
Reverse_Positions(myarray, arraysize);
Show_Array(myarray, arraysize);
return 0;
}
int Fill_Array(double ar[], int size)
{
int i = 0;
cout << "\nEnter a double value (Enter q to quit): ";
while (cin >> ar[i] && i < size)
{
i++;
cout << "Enter next value: ";
}
return i;
}
void Show_Array(const double ar[], int size)
{
cout << "\nHere is your array: " << endl << endl;
for (int i = 0; i < size; ++i)
cout <<"\#"<<i+1<<" is "<<ar[i] << endl;
}
void Reverse_Array(double ar[], int size)
{
cout << "Now we reverse the whole array: " << endl;
for (int i = 0; i != size / 2; ++i)
{
double temp;
temp = ar[i];
ar[i] = ar[size - 1 - i];
ar[size - 1 - i] = temp;
}
}
void Reverse_Positions(double ar[], int size)
{
cout << "Enter two positions: " << endl;
int a, b;
if (cin >> a >> b)
{
double temp = ar[b - 1];
ar[b - 1] = ar[a - 1];
ar[a - 1] = temp;
}
}
Something wrong with reverse_position function: It only shows "Enter two positions" and then the function terminates. There is no chance for me to enter the integers a and b. I doubt there is something wrong with the input queue, but I am not sure.