Hi guys :)

I have small question of course I don't want to ask for solving my homework, no i'll work on it on my own.

I just need some tips :) please.

I want to make a program that calculate these function:
F1 = 1;
F2 = 2*lgn + 2;
F3 = 2*n + 2;
F4 = 2*n*lgn + 2;
F5 = 2*n^2 + 2;
F6 = 2*(n!) + 2;
F7 = 2*(2^n) + 2;

for these values: (replace "n" with these values)
array[7] = {0,1,2,3,4,5,6}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

my question is, how should I write the could so that it'll replace "n" with "0,1,2,3,4,5,6" and get the result for each integer from that several functions.

Also, what should I write instead of "lg n"? I meant if I wrote in C++ "lg n" it won't understand that!!!


Thanks in advance..

This is my code at the moment

// Mohammed Aldhaferi
// 205112766
// Program that compute the value for some integers in several functions such as (1, log n, n...etc).

#include <iostream> // input\output stream.

#include <cmath> // To be able to use Math Library.

using namespace std; // This command will help us to not write "std::" all the time.


int main()
{
	int array[7] = {0, 1, 2, 3, 4, 5, 6}; // Initiating an array of size 7, of type integer.
	float F1, // Creating these
		F2, // variables
		F3, // to
		F4, // assgin to
		F5, // them
		F6, // the functions
		F7; // we want.

	cout << "We'll have these following functions:\n";
	cout << "F1 = 1\nF2 = 2*logn + 2\nF3 = 2*n + 2\nF4 = 2*nlogn + 2\nF5 = 2*n^2 + 2\nF6 = 2*(n!) + 2\nF7 = 2*(2^n) + 2\n\n";

	/*F1 = 1;
	F2 = 2 * logn + 2;
	F3 = 2 * n + 2;
	F4 = 2 * n * logn + 2;
	F5 = 2 * ( pow(n, 2) ) + 2;
	F6 = 2 * (n!) + 2;
	F7 = 2 * ( pow(2, n) ) + 2;*/

	// Output the head of the table we want to create.
	cout << "no.		" << "F1		" << "F2		" << "F3		" << "F4		" << "F5		" << "F6		" << "F7		";

	for( int i = 0; i < 7; i++ )
	{

	}


	return 0; // to indicate successfully termination.

} // end function main.

Recommended Answers

All 11 Replies

Member Avatar for onaclov2000

With regard to the n part of the question:
I would say use a for loop.
Since you have the specific n values in the array, just loop through the array from 0 to the number of items you have/want to use in your array.
I.E. F3 = 2 * array + 2

same concept for the rest of the for loop,
I'm really not sure as far as the log or ln functions go as I've never used them, I would say take a look in cmath at the actual class?

Someone else can probably give you a better answer for that.


I hope this makes sense....
Have a good day

Member Avatar for onaclov2000

One more thing, you might want to look out for and I don't know if this will be a problem, but if you try to use the array in the equation it might need you to type cast it, although I am not sure, so watchout for that if you're getting erroneous data.

With regard to the n part of the question:
I would say use a for loop.
Since you have the specific n values in the array, just loop through the array from 0 to the number of items you have/want to use in your array.
I.E. F3 = 2 * array + 2

same concept for the rest of the for loop,
I'm really not sure as far as the log or ln functions go as I've never used them, I would say take a look in cmath at the actual class?

Someone else can probably give you a better answer for that.


I hope this makes sense....
Have a good day

Thanks for the comments and the tips :)

Umm, Do you know how to use factorial from the cmath? OR I should make a function that calculates it?

Member Avatar for onaclov2000

I really haven't been programming long, I don't know if there is a factorial function, I actually had to write a factorial function in my class (school that is...LOL)a few months ago, I've attached it since I happen to keep my pen drive on me at all times (dork I know)

Mine happens to only allow up to and including the value 12, some simple changes will allow any number, although I never tested it with negative numbers, technically you can't factorial with negative numbers, but I think you should be able to, but your value will change from positive to negative depending on the number that you try to "negative factorial"

Hopefully it helps, and doesn't confuse you!!!

long factorial(int x)
{
   // This function finds the factorial of a number

   //declare the variable
   long factorialValue = x;
   //check the number for validity
   if (x == 1 || x == 0)
   {
      return 1;
   }
   else if ((x < 13) && (x > 1))
   {
      
      //If the number passes the validity test run the factorial
      for (int i = x-1; i>0; i--)
      {
         factorialValue = factorialValue * i;
      }
      return factorialValue;
   }
   else
   {
      cout << "You have selected an incorrect number please re-run the program"<< endl;
      return 0;
   }

}
commented: Thanks :) +1

Well, thanks a lot I guess I worked on your advice I built up my factorial function LOL :D

btw, this is the final code I finished it, but I got stuck in using "log n"

try to run it

// Program that compute the value for some integers in several functions such as (1, log n, n...etc).

#include <iostream> // input\output stream.

#include <cmath> // To be able to use Math Library.

using namespace std; // This command will help us to not write "std::" all the time.

// Defining function that calculates the factorial.
double factorial ( double );


int main()
{
	int array[7] = {0, 1, 2, 3, 4, 5, 6}; // Initiating an array of size 7, of type integer.
	double F1, // Creating these
		F2, // variables
		F3, // to
		F4, // assgin to
		F5, // them
		F6, // the functions
		F7; // we want.

	cout << "We'll have these following functions:\n";
	cout << "F1 = 1\nF2 = 2*logn + 2\nF3 = 2*n + 2\nF4 = 2*nlogn + 2\nF5 = 2*n^2 + 2\nF6 = 2*(n!) + 2\nF7 = 2*(2^n) + 2\n\n";

	/*F1 = 1;
	F2 = 2 * logn + 2;
	F3 = 2 * n + 2;
	F4 = 2 * n * logn + 2;
	F5 = 2 * ( pow(n, 2) ) + 2;
	F6 = 2 * (n!) + 2;
	F7 = 2 * ( pow(2, n) ) + 2;*/

	// Output the head of the table we want to create.
	cout << "no.	" << "F1	" << "F2	" << "F3	" << "F4	" << "F5	" << "F6	" << "F7	\n";

	for( int i = 0; i < 7; i++ )
	{
		F1 = 1;
		F2 = 2 * log (array[i]) + 2;
		F3 = 2 * array[i] + 2;
		F4 = 2 * array[i] * log (array[i]) + 2;
		F5 = 2 * pow(array[i], 2) + 2;
		F6 = 2 * factorial( array[i] ) + 2;
		F7 = 2 * pow(2, array[i]) + 2;

		cout << array[i] << "	" << F1 << "	" << F2 << "	" << F3 << "	" << F4 << "	" << F5 << "	" << F6 << "	" << F7 << endl;
	}

	cout << endl; // To output a new line.


	return 0; // to indicate successfully termination.

} // end function main.


// Function factorial to compute the factorial of a given number.
double factorial( double n )
{
	if( n == 0 || n == 1 )
		return 1;

	else
		return n * factorial( n - 1 );

}

Anyone can help in getting the correct value of "lg n"?

Member Avatar for onaclov2000

Not sure if this will help but here goes:
http://www.cplusplus.com/reference/clibrary/cmath/log.html

Looks like you can just call log(n) and it should work?

I'm not sure why it's not working, are you trying to use a different base or something? (I forget how log works...LOL)

Thanks guys I did it.

Here's the fixed completed code.

// Mohammed Aldhaferi
// 205112766
// Program that compute the value for some integers in several functions such as (1, log n, n...etc).

#include <iostream> // input\output stream.

#include <cmath> // To be able to use Math Library.

using namespace std; // This command will help us to not write "std::" all the time.

// Defining function that calculates the factorial.
double factorial ( double );


int main()
{
	int array[7] = {0, 1, 2, 3, 4, 5, 6}; // Initiating an array of size 7, of type integer.
	double F1, // Creating these
		F2, // variables
		F3, // to
		F4, // assgin to
		F5, // them
		F6, // the functions
		F7; // we want.

	cout << "We'll have these following functions:\n";
	cout << "F1 = 1\nF2 = 2 * log n + 2\nF3 = 2 * n + 2\nF4 = 2 * n * log n + 2\nF5 = 2 * n^2 + 2\nF6 = 2 * (n!) + 2\nF7 = 2 * (2^n) + 2\n\n";

	/*F1 = 1;
	F2 = 2 * logn + 2;
	F3 = 2 * n + 2;
	F4 = 2 * n * logn + 2;
	F5 = 2 * ( pow(n, 2) ) + 2;
	F6 = 2 * (n!) + 2;
	F7 = 2 * ( pow(2, n) ) + 2;*/

	// Output the head of the table we want to create.
	cout << "no.	" << "F1	" << "F2	" << "F3	" << "F4	" << "F5	" << "F6	" << "F7	\n";

	for( int i = 0; i < 7; i++ )
	{
		F1 = 1;

		if( array[i] == 0 )
			F2 = 2 * 0 + 2;
		else
			F2 = 2 * log ( array[i] ) + 2;
		F3 = 2 * array[i] + 2;
		
		if( array[i] == 0 )
			F4 = 2 * array[i] * 0 + 2;
		else
			F4 = 2 * array[i] * log (array[i]) + 2;
		F5 = 2 * pow(array[i], 2) + 2;
		F6 = 2 * factorial( array[i] ) + 2;
		F7 = 2 * pow(2, array[i]) + 2;

		cout << array[i] << "	" << F1 << "	" << F2 << "	" << F3 << "	" << F4 << "	" << F5 << "	" << F6 << "	" << F7 << endl;
	}

	cout << endl; // To output a new line.


	return 0; // to indicate successfully termination.

} // end function main.


// Function factorial to compute the factorial of a given number.
double factorial( double n )
{
	if( n == 0 || n == 1 )
		return 1;

	else
		return n * factorial( n - 1 );

}

Not sure if this will help but here goes:
http://www.cplusplus.com/reference/clibrary/cmath/log.html

Looks like you can just call log(n) and it should work?

I'm not sure why it's not working, are you trying to use a different base or something? (I forget how log works...LOL)

Thanks a lot for the help, I did it ;)

Member Avatar for onaclov2000

Also you may want to take a look at this for setw
http://www.cplusplus.com/reference/iostream/manipulators/setw.html

Basically you wouldnt have to do the " " in all the places you do, and it will set the width of the "string" I believe,
May make it easier to get the desired result you are looking for (column format I assume?)

Member Avatar for onaclov2000

Glad I could help!!

Also you may want to take a look at this for setw
http://www.cplusplus.com/reference/iostream/manipulators/setw.html

Basically you wouldnt have to do the " " in all the places you do, and it will set the width of the "string" I believe,
May make it easier to get the desired result you are looking for (column format I assume?)

Thanks a lot, I know that method and it won't work as great as the " " because it uses strings and sometimes they won't be equal.

Thanks..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.