MY CODE along with the output
The following code is not working. It has no errors, am doing some mistake in the logic I think. I want to find power of a number using functions. How to make this code work?
The code:
#include<iostream>
using namespace std;
int pow(int);
int main()
{
int x,p,ans;
cout<<"Enter a number";
cin>>x;
cout<<"Enter the power of the number";
cin>>p;
ans=pow(x);
cout<<ans;
return 0;
}
int pow(int)
{
int a=1,i,p,x;
for(i=0;i<=p;i++)
{
a=a*x;
}
return a;
}
Here is working code:
#include<iostream>
using namespace std;
int pow(int, int);
int main()
{
int x,p,ans;
cout<<"Enter a number";
cin>>x;
cout<<"Enter the power of the number";
cin>>p;
ans=pow(x, p);
cout<<ans;
return 0;
}
int pow(int x, int p)
{
int a=1,i;
for(i=0;i<=p;i++)
{
a=a*x;
}
return a;
}
Ideone
You have to pass the local variables into the function instead of defining new ones with the same name. What you are doing should give you warnings about unused variables (x and p in main) and it also invokes undefined behavior in pow because of ininitialized reads of the variables defined there.
Also your function was wrong. You were just multiplying 1 with a value a bunch of times, which stays 1 forever.
Your function must have the parameters names specified (not just the types):
int pow(int) -> int pow(int b, int p)
You iterate once more than necessary:
for (i = 0; i <= p; i++) -> for (i = 0; i < p; i++)
You can shorten some arithmetic operations:
a=a*x -> a *= x;
The final function:
int pow(int b, int p)
{
int a = 1, i;
for (i = 0; i < p; i++)
a *= b;
return a;
}
You call it by passing the variables precedently declared:
pow(x, p)
So your final code be like:
#include <iostream>
int pow(int b, int p)
{
int a = 1, i;
for (i = 0; i < p; i++)
a *= b;
return a;
}
int main()
{
int x, p, ans;
std::cin >> x >> p;
ans = pow(x, p);
std::cout << ans << std::endl;
return 0;
}
Related
#include <bits/stdc++.h>
using namespace std;
int myfunc(int n, int m, int k, int arr[], int arr1[]) {
int x = 0, y = 0;
for (int i = 0; i < n; ++i) {
if (arr[i] > k) {
x += 1;
}
}
for (int i = 0; i < m; ++i) {
if (arr1[i] > k) {
y += 1;
}
}
return max(x, y); // why is this not working?
}
int main() {
int n, m, k;
int arr[n];
int arr1[m];
cin >> n >> m >> k;
cout << "first: \n";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
cout << "2nd arr\n";
for (int i = 0; i < m; ++i) {
cin >> arr1[i];
}
myfunc(n, m, k, arr, arr1);
return 0;
}
In this code function is not returning any value but when I use cout<<max(x,y) instead of return max(x,y), it's working fine. Can anyone explain me why is this happening?
Your confusion seems to be arising from a misunderstanding of what it means to return a value from a function versus printing the value inside the function. Consider the following function that prints the right answer:
void f()
{
std::cout << 42;
}
Fine, this works and can be called like f();, and it prints the values.
On the other hand, if the function were to return a value, like this:
int f()
{
return 42;
}
then simply calling the function f(); is not going to do anything because you are not using the returned value. Instead, you need to do something like:
std::cout << f();
From C++17, you can use an attribute for a function that returns a value, indicating that the returned value is meant to be used, and that not using it is a mistake:
[[nodiscard]] int f()
{
return 42;
}
This has the nice property that a call like f(); will throw a warning, and the only way to avoid the warning is to actually use the returned value.
ive been trying to add 2 large integer on c++ using array and the output is always 0000 and i cant identified the problem
what im trying to achieve is like 30000000000 + 50000000 = 30050000000.
and this is the code that i wrote
, can anyone help me trying to figure it out why the output is 0000?
#include <math.h>
using namespace std;
int array1[100];
int array2[100];
int hasil[100];
int func1(int n)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array1[b]=n%10;
n=n/10;
b--;
}
int func2(int p)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array2[b]=p%10;
p=p/10;
b--;
}
int func3(int i)
{ int satuan,puluhan,bil;
for(i=sizeof(array2[100]);i>0;i--)
{
int bil = array1[100]+array2[100];
satuan=bil%10;
hasil[i]=satuan+puluhan;
puluhan=bil/10;
}
}
int main ()
{ int n,p,i;
int func1(n);
int func2(p);
int func3(i);
int satuan=0;
int puluhan=0;
int x,y;
cout<<"masukan bilangan pertama = ";
cin>>n; cout<<endl;
cout<<"masukan bilangan ke dua = ";
cin>>p; cout<<endl;
for(x=sizeof(hasil[100]);x>0;x--)
{
y=0;
cout<<hasil[y];
y++;
}
} ```
I have to calculate the sum of all numbers that end in either 3, 5 or 7 and output it in main(). I've done that but with the input file being:
5
35
2
3
28
16
17
I get 535030017 as output. It just doesn't sum them up but it cout's them. Here is my C++:
#include<iostream>
using namespace std;
void sumNumbers(int number){
int sum = 0;
if (number%10==3||number%10==5||number%10==7){
sum += number;
}
cout << sum;
}
int main ()
{
int x;
while(cin >> x){
sumNumbers(x);
}
}
The following should fix your issue:
#include<iostream>
using namespace std;
void sumNumbers(int& sum, int number)
{
if (number%10==3||number%10==5||number%10==7)
{
sum += number;
}
cout << sum << endl;
}
int main ()
{
int x;
int sum = 0;
while(cin >> x)
{
sumNumbers(sum, x);
}
return 0;
}
If you need sum then you should do like this.
#include<iostream>
using namespace std;
int sumNumbers(int sum, int number){
if (number%10==3||number%10==5||number%10==7){
sum += number;
}
cout << sum;
return sum;
}
int main ()
{
int sum = 0;
int x;
while(cin >> x){
sum = sumNumbers(sum, x);
}
}
Try this :-
#include<iostream>
using namespace std;
int sumNumbers(int sum, int number){
if (number%10==3||number%10==5||number%10==7){
sum += number;
}
return sum;
}
int main ()
{
int x, sum=0;
while (cin >> x){
sum = sumNumbers(sum, x);
}
cout<<"Sum of the numbers ending with 3,5 and 7 is "<<sum;
}
And i would suggest you to put a limit on the input by the user otherwise the program will keep on running till user manually breaks it.
This will work finally only when the loop breaks.
The problem is that sum is a new variable for each invocation of sumNumbers, so you start over at zero every time.
A slightly different approach, separating filtering, summing , and output:
int filterNumber(int number)
{
int ones = number % 10;
if (ones == 3 || ones == 5 || ones == 7)
{
return number;
}
else
{
return 0;
}
}
int main()
{
int sum = 0;
int x = 0;
while(cin >> x){
sum += filterNumber(x);
}
std::cout << sum << std::endl;
}
This question already has answers here:
Why is my HelloWorld function not declared in this scope?
(11 answers)
Closed 8 years ago.
I am working on a C++ project and while compiling, I am receiving the error messages:
error: mean was not declared in this scope
error: standard_dev was not declared in this scope
My code is
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int N(0);
char filename[100];
double m, stdev;
string temp;
double next;
int count = 0;
cout << "Enter name of file: ";
cin >> filename;
ifstream myfile;
myfile.open(filename);
while (myfile >> next)
{
count++;
}
N = count;
double* mydata;
mydata = new double[N];
for (int i = 0; i < N; i++)
{
myfile >> mydata[i];
}
m = mean(mydata, N);
stdev = standard_dev(mydata, m, N);
cout << "The standard deviation is:" << stdev << endl;
myfile.close();
delete[] mydata;
return 0;
}
double mean(double* mydata, double N)
{
double sum(0), m;
for (int i = 0; i < N; i++)
{
sum += mydata[i];
}
m = (sum / (double) N);
return (m);
}
double standard_dev(double* mydata, double m, int N)
{
double* mydata2 = new double[N];
for (int i = 0; i < N; i++)
{
mydata2[i] = pow((mydata[i] - m), 2);
}
double sum(0), S, X;
for (int i = 0; i < N; i++)
{
sum += mydata2[i];
}
X = sum / N;
S = sqrt(X);
return (S);
}
Those functions have not been seen yet when you try to use them; the compiler doesn't know what they are which leads to the error. Either move them before main() or prototype them, such as:
double mean(double * mydata, double N);
double standard_dev(double * mydata, double m, int N);
int main()
{
...
This will give the compiler its expectations for those symbols, so when it sees them in use it knows what to do with them.
Any name must be defined before its using (with rare exceptions relative to template). You use name mean in statement
m = mean(mydata, N);
but name mean is not yet defined.
Place function declarations before main
double mean(double *mydata, double N);
double standard_dev(double *mydata, double m, int N);
int main()
{
//...
Or before their usage.
int main()
{
double mean(double *mydata, double N);
double standard_dev(double *mydata, double m, int N);
//...
m = mean(mydata, N);
//...
You need to have prototypes for your functions mean() and std_dev() at the top of your file. Otherwise, they don't 'exist' in the main function. Either copy the entire functions to the space above your main function, or make prototypes for them. Good luck!
# include <iostream>
using namespace std;
class mm
{
private:
int k[1000];
int n;
int i;
int a;
int b;
int f;
public:
mm ()
{
a=0;
b=1;
f=0;
i=0;
for(int i=0; i<n;i++)
k[i]=0;
};
~mm()
{
}
void fib(int n)
{
for (int i=0;i<n;i++)
{
if (i<=1)
f=i;
else
{
f=a+b;
a=b;
b=f;
}
k[i]=f;
}
for (int j=0;j<n;j++)
cout<<k[j]<<" ";
}
int se (int n, int i)
{
if (n==1)
return 1;
else
return 1/k[i] + se (n-1, i+1);
}
};
int main()
{
int n;
cout<<"Enter n:";
cin>>n;
mm p;
cout<<"fib: "<<endl;
p.fib(n);
cout<<endl;
cout<<"se: ";
cout<<p.se(n,0);
return 0;
}
Recursion function from main is not responding. Maybe the array k[i] is not working, but I cant find the reason. Can anyone help me?
k[0] is set to 0. When you then call se(n,0) in main, it computes 1/k[0] + se(n-1,1) which is a division by zero.