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;
}
Related
the code returns error: invalid types 'int[int]' for array subscript| and everything else seems to run fine. What can I do about it ? (line 10,22,23)
#include <iostream>
using namespace std;
int n, x[50], y[50], z[50];
void citire(int &n, int v)
{
int i;
cin >> n;
for (int i = 1; i <= n; i++)
{
cout << "v[" << i << "]=";
cin >> v[i];
}
}
void afisare(int n, int v[])
{
int i;
for (i = 1; i <= n; i++)
cout << v[i] << " " << endl;
}
void s(unsigned n, int x[], int y[], int z[])
{
int i;
for (i = 1; i <= n; i++)
z[i] = abs(x[i] - y[i]);
}
int main()
{
cout << "n=";
cin >> n;
cout << "x[]:" << endl;
citire(n, x);
cout << "y[]:" << endl;
citire(n, y);
cout << "Elementele primului vector" << endl;
afisare(n, x);
cout << "Elementele celui de-al doilea vector:" << endl;
afisare(n, y);
s(n, x, y, z);
cout << "z[]:" << endl;
return 0;
}
You are using v as an array, but it isn't an array, it is int. You probably want to use int* v.
void citire(int &n, int* v) {
// ...
cin>>v[i];
}
I have a compile error when using "output_array()" in a templated member function, the following code is a simple example:
#include <iostream>
using namespace std;
int menu()
{
int opt;
cout << "\n**********MENU**********";
cout << "\n1. Print the Array in reverse order";
cin >> opt;
return opt;
}
void input_array(int b[], int n)
{
cout << "\nEnter the elements of the array";
for (int i = 0; i < n; i++)
{
cin >> b[i];
}
return;
}
void output_array(int c[], int n)
{
for (int j = 0; j < n; j++)
{
cout << c[j] << " ";
}
return;
}
void rev_arr(int b[], int n)
{
int start = 0, end = n - 1;
cout << "Array before reversing : " << output_array(b, n);
for (int i = 0; i < n / 2; i++)
{
float temp = b[start];
b[start] = b[end];
b[end] = temp;
start++;
end--;
}
cout << "Array after reversing : " << output_array(b, n);
return;
}
void task(int b[], int n, int opt)
{
switch (opt)
{
case 1:
rev_arr(b, n);
break;
default:
cout << "!!! Sorry Wrong Choice !!!";
}
return;
}
int main()
{
int size;
int a[50];
cout << "\n You can Enter 50 elements in the array.\nHow much you want to enter?";
cin >> size;
input_array(a, size);
int opt = menu();
task(a, size, opt);
return 0;
}
The output_array(int*, int) is a printing function, not an opertot<< overlaod. Therefore, your can't place output_arrry(b, n) after operator<<. Simple separate into two statments:
Change
cout << "Array after reversing : " << output_array(b, n); // Not good.
into
cout << "Array after reversing : "; output_array(b, n); // ok.
Or you have to write an operator<< overloading, give rules for operator<< to print you array. But, before doing this, you have to bind the size n in your array (as some kind of structure). The size is needed to be used in the function operator<<.
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 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;
}
#include <iostream>
using namespace std;
long int A(int, int);
int main()
{
int m, n;
cout << "Enter two numbers for Ackerman's Function." << endl;
cin >> m >> n;
cout << A(m, n) << endl;
}
long int A(int m, int n)
{
if(m == 0)
{
return n+1;
}
else if(m > 0 && n == 0)
{
return A(m-1,1);
}
else if(m > 0 && n > 0)
{
int temp = A(m,n-1);
return A(m-1, temp);
}
}
Here is a simple code of an Ackerman's function. I am wondering how many times does this Ackerman's function calls itself as a function of n, if m is a constant? My brain exploded trying to figure it out.
you can use a count global variable to find out
#include <iostream>
using namespace std;
long int A(int, int);
int count=0;
int main()
{
int m, n;
cout << "Enter two numbers for Ackerman's Function." << endl;
cin >> m >> n;
cout << A(m, n) << endl;
count << " Ackerman's Function runs " << count << " times.";
}
long int A(int m, int n)
{
count++;
if(m == 0)
{
return n+1;
}
else if(m > 0 && n == 0)
{
return A(m-1,1);
}
else if(m > 0 && n > 0)
{
int temp = A(m,n-1);
return A(m-1, temp);
}
}
Add an extra parameter of type int& to your A function:
long int A(int m, int n, int& count)
{
count++;
And update the prototype, and the recursive calls to match.
In your main function:
int count = 0;
cout << A(m, n, count) << endl;
cout << "A calls: " << count << endl;
This avoids globals, which are problematic.