With this program I'n trying achieve an output that looks something like this
A+B+C= 7
xMin = 3
xMax = 8
3----10
4----11
5----12
6----13
7----14
8----15
Instead I usually get something like this
4----0
5----0
6----0
7----0
8----0
It only changes when I hard code xMin or xMax to display, all the in-bewteens don't show.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int X = 0;
double a, b, c, xMin, xMax;
double y = 0;
cout << "#1(A): ";
cin >> a;
cout << "\n#2(B): ";
cin >> b;
cout << "\#3(C): ";
cin >> c;
cout << "Enter Xmin" << endl;
cin >> xMin;
cout << "Enter Xmax" << endl;
cin >> xMax;
y = a + b + c + X;
for (int count = xMin; count <= xMax; count++)
{
cout << count << "\t" << y << "\n";
}
return 0;
}
Your for loop is wrong (you are not updating the upper bound), change it to :
y = a + b + c;
for (int count = xMin; count <= xMax; count++)
{
cout << count << "\t" << count + y << "\n";
}
#include <iostream>
#include <cmath>
using namespace std;
struct updInt{
int xMax;
int xMin;
int inc;
int val;
bool flag;
friend ostream& operator<<(ostream& os, updInt& dt){
os <<dt.val;
dt.val+=dt.inc;
if(dt.val>dt.xMax)
dt.flag=true;
if(dt.val<dt.xMin)
dt.flag=true;
return os;
}
updInt(int a,updInt X,int inc=1){
this->val=a+X.val;
this->xMax = a + X.xMax;
this->xMin = a + X.xMin;
this->inc =inc;
flag=false;
}
updInt(int max,int min,int val,int inc=1){
this->val= val;
this->xMax = max;
this->xMin = min;
this->inc = inc;
flag=false;
}};
int main(){
int a, b, c, xMin, xMax;
cout << "#1(A): ";
cin >> a;
cout << "\n#2(B): ";
cin >> b;
cout << "\#3(C): ";
cin >> c;
cout << "Enter Xmin" << endl;
cin >> xMin;
cout << "Enter Xmax" << endl;
cin >> xMax;
updInt X(xMax,xMin,0);
updInt y(a + b + c , X);
for (int count = xMin; count <= xMax; count++)
{
cout << count << "\t" << y << "\n";
if(y.flag)
break;
}
return 0;
}
You need to write your ostream operator when you want to increase it without touching at the loop but i dunno why dont you simply increase y too.
Related
here is my code, as you can see I used arrays of structures. The question is how should I take the arrays element by user directly? You can see below how the elements are given to the program before execution.
any help will be appericiated.
#include<iostream>
#include<math.h>
using namespace std;
struct Data
{
int x, y;
};
double interpolate(Data f[], int xi, int n)
{
double P = 0;
for (int i = 0; i<n; i++)
{
double p= f[i].y;
for (int j = 0;j<n;j++)
{
if (j != i)
p =p*(xi - f[j].x) / (f[i].x - f[j].x);
}
P += p;
}
return P;
}
int main()
{
Data f[] = { { 0,2 },{ 1,3 },{ 2,12 },{ 5,147 }};
cout << "Value of f(3) is : " << interpolate(S, 3, 4) << endl;
system("pause");
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
struct Data
{
int x, y;
};
double interpolate(Data f[], int xi, int n)
{
double P = 0;
for (int i = 0; i<n; i++)
{
double p= f[i].y;
for (int j = 0;j<n;j++)
{
if (j != i)
p =p*(xi - f[j].x) / (f[i].x - f[j].x);
}
P += p;
}
return P;
}
int main()
{
int input[8];
cout << "Enter first value: \n";
cin >> input[0];
cout << "Enter second value: \n";
cin >> input[1];
cout << "Enter third value: \n";
cin >> input[2];
cout << "Enter fourth value: \n";
cin >> input[3];
cout << "Enter fifth value: \n";
cin >> input[4];
cout << "Enter sixth value: \n";
cin >> input[5];
cout << "Enter seventh value: \n";
cin >> input[6];
cout << "Enter eighth value: \n";
cin >> input[7];
Data f[] = {{input[0], input[1]}, {input[2], input[3]}, {input[4], input[5]}, {input[6], input[7]}};
cout << "Value of f(3) is : " << interpolate(f, 3, 4) << endl;
system("pause");
return 0;
}
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;
}
}
I want the average of the two biggest variables among the three variables n1, n2, n3. Can someone help me. I ask the user to enter three notes will be stored in variables n1, n2, n3. then I want the program to return the average of the two biggest variables.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
float ra[23], qte_alunos=0;
float n1[29],n2[33],n3[33],op1[22],op2[22], fina[22];
string nome[23], curso[23];
for (int i=0; i<3; i++){
cout << "digite RA: ";
cin >> ra[i];
cout << "digte nome: ";
cin >> nome[i];
cout << "digite curso: ";
cin >> curso[i];
cout << "digite N1: ";
cin >> n1[i];
cout << "digite N2: ";
cin >> n2[i];
cout << "digite N3: ";
cin >> n3[i];
if (n1[i] > n2[i] && n2[i] > n3[i]){
n1[i] = op1[i];
n2[i] = op2[i];
}
if (n2[i] > n3[i] && n3[i] > n1[i]){
n2[i] = op1[i];
n3[i] = op2[i];
}
if (n3[i] > n1[i] && n1[i] > n2[i]){
n3[i] = op1[i];
n1[i] = op2[i];
}
fina[i] = (op1[i]+op2[i])/2;
if (fina[i] > 6 ){
cout << "aprovado " << fina[i];
}
if (fina[i] > 4 && fina[i] < 5.9){
cout << "exame " << fina[i];
}
if (fina[i] < 4){
cout << "reprovado " << fina[i];
}
cout << "\n" << endl;
}
return 0;
}
If I get you right you want (sum(a, b, c) - min(a, b, c)) / 2:
#include <algorithm>
#include <iostream>
int main (int argc, const char **argv) {
double a = 1;
double b = 2;
double c = 3;
double min = std::min({a, b, c});
// double max = std::max({a, b, c});
double sum = a + b + c;
// double result = ((sum - min - max) + max) / 2;
// which is:
double result = (sum - min) / 2;
std::cout << result << '\n';
}
I would like to analyze the complexity of my code algorithm.Therefore,i must have 2 different programs giving the same functions to allow me to start off.
Currently this is my own code.
I'm not sure if it is allowed that i would like to have someone that could volunteer his own way code to compute summation of factorial for me as the 2nd program code.
Preferrably a nested loop.
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++)
{
c = c * i;
a = a + c;
}
cout << "The sum of the factorials is " << a << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int val;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
static const int results[] = {
0, 1, 3, 9, 33, 153, 873, 5913, 46233, 409113,
4037913, 43954713, 522956313
};
cout << "The sum of the factorials is " << results[val < 0 ? 0 : val] << endl;
system("pause");
return 0;
}
Note that I replicated the defect in the original program which causes it to return the incorrect value if the user enters 0.
This alternate version assumes 32-bit integers because it takes advantage of overflow behavior. Extending to 64-bit integers is left as an exercise.
I do not understand what you do with another nested way but i hope this can help...
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++){
c *= i;
a += c;
}
int c2=1;
for (i = val; i > 1; i--){
c2*=i;
c2++;
}
cout << "The sum of the factorials is " << a << endl;
cout << "The sum of the factorials is " << c2 << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int suma = 0;
int n = 0;
cout << "Sum of factorials\n";
cout << "-------------------------------\n";
cout << "Insert number of n: ";
cin >> n;
int i = 1;
while (i <= n)
{
int factorial = 1;
for(int j=1; j<=i; j++)
{
factorial = factorial * j;
}
suma += factorial;
i++;
}
cout << "Sum of factorials is: " << suma;
system("pause");
return 0;
}
Here is a code I have written, although there is no double variable in it, g++ throws this warning: "invalid operands of types ‘double’ and ‘int’ to binary ‘operator%’
c = pow(m,e)%n;
" How is that?
#include <iostream>
#include <boost/dynamic_bitset.hpp>
#include <math.h>
using namespace std;
int main()
{
boost::dynamic_bitset <> seq(5);
int key = 0;
int p = 0, q = 0;
int n = 0;
int f = 0;
int d = 0;
int e = 0;
int c = 0;
int m = 0;
int t = 0;
cout << "Enter a sequence of bits: ";
cin >> seq;
for (unsigned int i = 0; i < seq.size(); i++)
{
if ( seq[i]==1)
{
d = d + pow(2,i);
cout << d << "\n";
}
}
//cout << key << "\n";
cout << "Enter p: ";
cin >> p;
cout << "Enter q: ";
cin >> q;
cout << "Enter m: ";
cin >> m;
n = p*q;
f = (p-1)*(q-1);
for ( int k = 0; t < 1; k++)
{
if ((1+k*f)%d==0)
{
t = 2;
e = (1+k*f)/d;
}
}
cout << "E is: " << e << "\n";
c = pow(m,e)%n;
cout << "C is: " << c << "\n";
cin.get();
return 0;
}
Function pow is declared as
double pow (double base, double exponent)
that is a double.