My program basically asks the users for a value (s) then calculates and displays the First, Middle, Last, and Final. The users have a choice to enter the value either in integer, real or character. If the user enters it in real or character, it will be converted into integer value before the required value is computed. I have written the power , factorial and the sigma functions which I will use in calculating the first, middle and the last. Can somebody assist in checking my codes to see if its correct and show how I will those functions to calculate the first and the last which a bit more complicated?

First = X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n!
Middle = n sigma i = 1 sqrt(i/x)
last = 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - ....X^n/n!
Final = first/last + middle

#include<iostream>
#include<cmath>
using namespace std;


int powerfunc (int x, int n)
{
  int x, n;
  int  p, i;
while (i <=n)
 {
   p = p * x;
   i = i + 1;
   return p;
 }
}

int factorial (int n)
{
   int i, n;
   int f = 1;
for (i = 1; i <= f; i++)
  {
      f *= i;
     return f;
    }
}

int sigma(int x, int n)
{
   int i = 1, sum = 0;
   int x, n;
while (i <= n)
  {
     sum += i *  i;
      i++;
     sum = sqrt(sum/x)
      return sum;
    }
}

int main()
{
   
  int x, n;
  int first, middle, last, final;
cout << “Please enter the value for x” << endl;
cin >> x;
cout << “Please enter the value for n” << endl;
cin >>n;

 middle = sigma(x, n)
 final = fist/(last + middle);

 cout << “The value for first is << first << endl;
 cout << “The value for middle is << middle << endl; 
 cout << “The value for last is << last << endl;
 cout << “The value for final is << final << endl;







return 0;
}

Ok there are two types of problem here:
(a) that you have made errors in your code that result in you getting the wrong answer,
(b) I really don't think you know mathematically what you want to compute.

I can address (a) easily and I will hint at (b).

Let us discuss (a):

First off: Consider your powerfunc: I have added some comments.

int 
powerfunc (int x, int n)
{
int x, n;  // RE DEFINITION OF X,N : Mask the version above: delete this line!!
int p, i;   // p and i can be ANYTHING use int i(10); say to set it to 10.

while (i <=n)
{
  p = p * x;
  i = i + 1;
  return p;         // NO :: This returns in INSIDE the loop
}
   // Only get here if  i's initial value is > n.
   // Shouldn't functions return a value???
}

So the main problem you have is scope. Your { } define an range that a condition/ function works. e.g

for(int i=0;i<10;i++)
    {  
     // ALL THIS stuff gets done 10 times
     for(int j=20;j<30;j++)
       {
          // This stuff gets done 100 times. 
       }
    }

In the example you see that you have two lots of scope. The part that the outer for
loop does is one scope and the inner loop is another. The inner part is done 100 times because its is done 10 time each time through the outer loop.

Please experiment by putting cout<<"Here with i == "<<i<<endl; , type comments throughout your code , in particular IN your functions. You will be surprised I think to find that you don't call many of them.

Experiment, and post your updates.


Question: Are you using nasnoma's account? The code you posted a couple of years ago was an fairly sophisticated linked list -- this is much more complete beginners code -- you would not have forgotten stuff like i++ etc.

Edited 6 Years Ago by StuXYZ: n/a

Ok there are two types of problem here:
(a) that you have made errors in your code that result in you getting the wrong answer,
(b) I really don't think you know mathematically what you want to compute.

I can address (a) easily and I will hint at (b).

Let us discuss (a):

First off: Consider your powerfunc: I have added some comments.

int 
powerfunc (int x, int n)
{
int x, n;  // RE DEFINITION OF X,N : Mask the version above: delete this line!!
int p, i;   // p and i can be ANYTHING use int i(10); say to set it to 10.

while (i <=n)
{
  p = p * x;
  i = i + 1;
  return p;         // NO :: This returns in INSIDE the loop
}
   // Only get here if  i's initial value is > n.
   // Shouldn't functions return a value???
}

So the main problem you have is scope. Your { } define an range that a condition/ function works. e.g

for(int i=0;i<10;i++)
    {  
     // ALL THIS stuff gets done 10 times
     for(int j=20;j<30;j++)
       {
          // This stuff gets done 100 times. 
       }
    }

In the example you see that you have two lots of scope. The part that the outer for
loop does is one scope and the inner loop is another. The inner part is done 100 times because its is done 10 time each time through the outer loop.

Please experiment by putting cout<<"Here with i == "<<i<<endl; , type comments throughout your code , in particular IN your functions. You will be surprised I think to find that you don't call many of them.

Experiment, and post your updates.


Question: Are you using nasnoma's account? The code you posted a couple of years ago was an fairly sophisticated linked list -- this is much more complete beginners code -- you would not have forgotten stuff like i++ etc.

Actually I find it difficult solving mathematical problems using c++. I will be grateful if someone can help debug and correct the mistakes. Here is what I have tried


First = X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n!
Middle = n sigma i = 1 sqrt(i/x)
last = 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - ....X^n/n!
Final = first/last + middle

#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;


int factorial(int n)     // factorial function
{
if (n<=1)
return(1);
else
n=n*factorial(n-1); // notice we just keep calling this over until we get to 1!
return(n);
} 

int first (int n)   //  function for first
{
   
  while (n <= 1)
  {
     f = f + pow (x,n) / factorial (int n);
      return (f);
      n-=2;
    }
}

int last (int n)      //  function for last
{
   
  while (n <= 1)
  {
     l = l + 1 - pow (x,n) / factorial (int n);
      return (l);
      n-=2;
    }
}

int sigma(int x, int n)
{
   int i = 1, sum = 0;
   int x, n;
while (i <= n)
  {
     sum += i *  i;
      i++;
     sum = sqrt(sum/x)
      return sum;
    }
}

int main()
{
   
  int x, n;
  int first, middle, last, final;
  char ans;

do {
cout << “Please enter the value for x” << endl;
cin >> x;
cout << “Please enter the value for n” << endl;
cin >>n;

 First = first (n);
 Middle = sigma(x, n);
 Last = last (n);
 Final = Frist/(Last + Middle);

 cout << “The value for first is << first << endl;
 cout << “The value for middle is << middle << endl; 
 cout << “The value for last is << last << endl;
 cout << “The value for final is << final << endl;


cout << "Do you want to calculate this again? (y/n): ";
cin >> ans;
 
} while (ans == 'y' || ans == 'Y');
 
getch ();





return 0;
}
This article has been dead for over six months. Start a new discussion instead.