 Vcoderz Community (http://forum.vcoderz.com/index.php)
-   E-Learning Center (http://forum.vcoderz.com/forumdisplay.php?f=33)
-   -   C++ Help (http://forum.vcoderz.com/showthread.php?t=13889)

 bitarno 11-07-2008 07:43 PM

C++ Help

hey guys i'd like to receive some help by some programs in C++ thanks

I use Visual C++ so i hope the code would be convenient

1st Excersice

Write a complete C++ code in which you develop a function to compute n factorial
N factorial = n! = 1*2*3**n
In this code, you read the value of n from the user and you pass it to the function.

2nd Exercise

Consider the following series
x(n) = 1/n + 1/(n-1) +  + 1 for all n > 0
x(1) = 1
Develop a recursive function to compute the value of x(n)

3rd Exercise

Write a C++ code that generates a random number between -10 and +15

( this one we should use rand()%... something like that )

Thx Guys in advance :D
more to come later

 Sheriff Ice 11-07-2008 08:20 PM

Re: C++ Help

PM me i have them:p i solved them the first year of uni

i still have the solution on a word file :p

and i can give you an e-book that it may help you

 Google 11-07-2008 10:13 PM

Re: C++ Help

Quote:
 Originally Posted by icemaker (Post 158066) PM me i have them:p i solved them the first year of uni i still have the solution on a word file :p and i can give you an e-book that it may help you
Why not posting them here, so we can all benefit :p

Quote:
 more to come later
Waiting for this more :p
Bring us shi desem
:p

 Kingroudy 11-07-2008 10:25 PM

Re: C++ Help

1st exercise:
int n, nf=1;
cin>>n;
for(int i=1; i<=n, i++)
{
nf*=i;
}
cout<<nf

hayda L base taba3 L code.. it has been a full year i haven't touched c++, and i have an exam tomorrow so i can't stay long for this.

 Sheriff Ice 11-08-2008 09:59 AM

Re: C++ Help

Quote:
 Originally Posted by Edgard Chammas (Post 158092) Why not posting them here, so we can all benefit :p Waiting for this more :p Bring us shi desem :p
Ill post them :p ma te3tal ham

just give me till afternoon i am at work ill post them as i get home

 bitarno 11-08-2008 12:02 PM

Re: C++ Help

I want also a code for a program to change a number entered by a user into binary

 Google 11-08-2008 02:32 PM

Re: C++ Help

Decimal to Binary Converter:

Code:

#include <iostream.h>
void binary(int);
void main() {
int number;
cout << "Enter a positive integer: ";
cin >> number;
if (number < 0)
cout << "The number must be positive you idiot!\n";
else {
cout << number << " is ";
binary(number);
cout<<" in binary.\n\n";
}
}
void binary(int n) {
int rem;
if(n <= 1) {
cout << n;
return;
}
rem = n%2;
binary(n >> 1);
cout << rem;
}

 Tawa 11-08-2008 04:40 PM

Re: C++ Help

Quote:
 Originally Posted by bitarno (Post 158053) 2nd Exercise Consider the following series x(n) = 1/n + 1/(n-1) +  + 1 for all n > 0 x(1) = 1 Develop a recursive function to compute the value of x(n)
Code:

#include <iotream>
using namespace std;

double function(int);

int main(void)
{
int n;
double x;

do{
cout << "Enter n: ";
cin >> n;
}while(n<1);

x = function(n);

cout << "X(n) = " << x << endl;

return 0;
}

double function(int n)
{
if(n == 1)
return 1;
return 1/n + function(n-1);
}

Damn I Miss The C++ :p

 Sheriff Ice 11-08-2008 07:05 PM

Re: C++ Help

Code:

1.23

#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"enter your first integer:\n";
cin>>x;
cout<<endl<<"enter your second integer:\n";
cin>>y;
cout<<endl<<"the sum is:  "<<x+y<<endl<<"the product is:  "<<x*y<<endl<<"the difference is:  "<<x-y<<endl<<"the quotion is:  "<<x/y<<endl;
return 0;
}

1.24

#include<iostream>
using namespace std;
int main()
{
int x;
x=0;
if(x<=4)
cout<<x;
return 0;
}

1.25

#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"enter your first integer; \n";
cin>>x;
cout<<endl<<"enter your second integer; \n";
cin>>y;
cout<<endl;
if(x>y)
cout<<x<<" is larger"<<endl;
else if(y>x)
cout<<y<<" is larger"<<endl;
else if(x==y)
cout<<"these number are equal  "<<x<<" = "<<y<<endl;
return 0;
}

1.26

#include<iostream>
using namespace std;
int main()
{
int x,y,z,sum,ave,pro;
cout<<"Input three different integers: ";
cin>>x>>y>>z;
sum=x+y+z;
cout<<"sum is "<<sum<<endl;
ave=(x+y+z)/3;
cout<<"average is "<<ave<<endl;
pro=x*y*z;
cout<<"product is "<<pro<<endl;
return 0;
}

1.30

#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,e;
cout<<"enter the 5 values:\n";
cin>>a>>b>>c>>d>>e;
if(a<b,a<c,a<d,a<e)
cout<<"the smallest "<<a<<endl;
else if(b<a,b<c,b<d,b<e)
cout<<"the samallest "<<b<<endl;
else if(c<a,c<b,c<d,c<e)
cout<<"the smallest "<<c<<endl;
else if(d<a,d<b,d<c,d<e)
cout<<"the smallest "<<d<<endl;
else
cout<<"the smallest "<<e<<endl;
}
{
if(a>b,a>c,a>d,a>e)
cout<<"the largest "<<a<<endl;
else if(b>a,b>c,b>d,b>e)
cout<<"the largest "<<b<<endl;
else if(d>a,d>b,d>c,d>e)
cout<<"the largest "<<d<<endl;
else if(c>a,c>b,c>d,c>e)
cout<<"the largest "<<c<<endl;
else
cout<<"the largest "<<e<<endl;
return 0;
}

1.31

#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"enter your integer: \n";
cin>>x;
if(x%2)
cout<<x<<" is even\n";
else
cout<<x<<" is odd\n";
cout<<"enter your integer \n";
cin>>y;
if(y%2)
cout<<y<<" is even\n";
else
cout<<x<<" is odd\n";
return 0;
}

1.32:
x 1.32:

#include <iostream>
using namespace std;
int main()
{
int x,y;
cout<<"Enter two integers\n";
cin>>x>>y;
if (x%y==0)
cout<<x<<"is multiple of"<<y;
return 0;
}

<!--  /* Font Definitions */  @font-face        {font-family:&quot;Cambria Math&quot;;        panose-1:2 4 5 3 5 4 6 3 2 4;        mso-font-charset:1;        mso-generic-font-family:roman;        mso-font-format:other;        mso-font-pitch:variable;        mso-font-signature:0 0 0 0 0 0;}  /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal        {mso-style-unhide:no;        mso-style-qformat:yes;        mso-style-parent:&quot;&quot;;        margin:0in;        margin-bottom:.0001pt;        mso-pagination:widow-orphan;        font-size:12.0pt;        font-family:&quot;Times New Roman&quot;,&quot;serif&quot;;        mso-fareast-font-family:&quot;Times New Roman&quot;;        mso-bidi-language:AR-LB;} .MsoChpDefault        {mso-style-type:export-only;        mso-default-props:yes;        font-size:10.0pt;        mso-ansi-font-size:10.0pt;        mso-bidi-font-size:10.0pt;} @page Section1        {size:8.5in 11.0in;        margin:1.0in 1.0in 1.0in 1.0in;        mso-header-margin:.5in;        mso-footer-margin:.5in;        mso-paper-source:0;} div.Section1        {page:Section1;} -->
EX:2.16:
#include<iostream> //chapter 2 ex2.16
using namespace std;
int main()
{
double galon,mil,total,m,ave;
m=0;
total=0;
cout<<"enter the gallons used, -1 to end: \n";
cin>>galon;
while(galon!=-1)
{
total=total+galon;
cout<<"enter miles driven: \n";
cin>>mil;
m=m+mil;
cout<<"the miles/gallons for this tank was "<<mil/galon; //average for one entry
cout<<endl<<"enter the gallons used, -1 to end: \n";
cin>>galon;
}
ave=(m/total);
cout<<"the overall average is: "<<ave<<endl; //the overall average.
return 0;
}
Ex:2.17:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int an;
double bb,tc,cr,cl,b;
cout<<"Enter the account number: (-1 to end)"<<endl;
cin>>an;
while(an!=-1)
{
cout<<"Enter the beginning balance: "<<endl;
cin>>bb;
cout<<endl<<"Enter total charges:"<<endl;
cin>>tc;
cout<<endl<<"Enter total credits: "<<endl;
cin>>cr;
cout<<endl<<"Enter credit limit: "<<endl;
cin>>cl;
cout<<endl;
b=bb+tc-cr;
if(b>=cl)
{
cout<<"Account"<<setw(8)<<an<<endl;
cout<<"credit limit"<<setw(4)<<cl<<endl;
cout<<"Balance"<<setw(8)<<b<<endl;
cout<<"Credit limit exceeded"<<endl;
}
cout<<endl<<endl;
cout<<"enter the account number: (-1 to end)"<<endl;
cin>>an;
}
return 0;
}

Ex:2.18:
#include<iostream>//exercise2.18 page 160
using namespace std;
int main()
{
int ws;
double sales,total,commission;
ws=200;
cout<<"enter sales in dollars (-1 to end):"<<endl;
cin>>sales;
while(sales!=-1)
{
commission=(9*sales)/100;
total=ws+commission;
cout<<"salary is: \$"<<total<<endl<<endl;
cout<<"enter sales in dollars (-1 to end):"<<endl;
cin>>sales;
}
return 0;
}

Ex:2.19:
#include<iostream>
using namespace std;
int main()
{
int hours,ratelow,extra;
double ratehigh,bonus;;
cout<<"enter hours worked (-1 to end)\n";
cin>>hours;
while(hours!=-1)
{
cout<<"enter hourly rate of the worker(\$00.00): "<<endl;
cin>>ratelow;
ratehigh=ratelow*1.5;
if(hours>40)
{
extra=hours-40;
bonus=extra*ratehigh;
cout<<"the salary is:\$"<<bonus+40*ratelow<<endl;
}
else
cout<<"the salary is:\$"<<hours*ratelow<<endl<<endl;
cout<<"enter hours worked (-1 to end)"<<endl;
cin>>hours;
}
return 0;
}

Ex:2.20:
#include<iostream>
using namespace std;
int main()
{
int counter=0,number=0,large=0;
cout<<"enter number of sold objects\n";
cin>>number;
while(counter<=10)
{
counter=counter+1;
if(number>=large)large=number;
cout<<"enter the number of sold objects\n";
cin>>number;
}
cout<<"the most sold is "<<large;
return 0;
}

Ex:2.21:
#include<iostream>
using namespace std;
int main()
{
int x;
x=1;
cout<<"N\t10*N\t100*N\t1000*N\n\n";
while(x<=5)
{
cout<<x<<"\t"<<x*10<<"\t"<<x*100<<"\t"<<x*1000<<endl;x++;
}
return 0;
}

problem 1:

#include <iostream>
using namespace std;
void prime(int,int);
int main()
{
int a,b;
cout<<"enter the domain between 2 numbers u need to find the prime numbers:"<<endl;
cin>>a>>b;
prime(a,b);
return 0;
}

void prime(int a,int b)
{
int i;
int num=0;

for (int j=a;j<=b;j++)
{
num=num+1;

for (i=2; i<num; i++)
{
if((num%i)==0)
{
break;
}

}

if (i >= num)
{
cout<<num<<endl;
}
}
}

problem 2

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const int maxNumb = 10000;
int sum, largestFactor, divisor;
double started, ended;
for (int lcv = 2; lcv < maxNumb; ++lcv)
{
sum = 1;
largestFactor = (int)sqrt((double)lcv);
for (divisor = 2; divisor <= largestFactor; ++divisor)
{
if (lcv % divisor == 0)
{
sum += divisor;
if (divisor * divisor != lcv)
sum += (lcv / divisor);
}
if (sum > lcv) break;
}
if (sum == lcv) cout << lcv << endl;
}

return 0;
}

problem 4

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x,y,z,a;
while(a!=-1)
{
x=(1+rand() %9);
y=(1+rand() %9);
z=x*y;
cout<<"how much is "<<x<<" times "<<y<<" ?"<<endl;
cin>>a;
while (a!=z)
{
cout<<endl<<"No, plz try again"<<endl;
cin>>a;
}

cout<<"very good"<<endl;
cout<<"enter -1 to exit or any number to continue";
cin>>a;
}
return 0;
}

problem 5

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x,y; char c;
cout<<"want to play (y or n)"<<endl;
cin>>c;
while(c==y)
{
x=(1+rand()%1000);
cout<<"i have a number between 1 and 1000."<<endl;
cout<<"can you guess my number?"<<endl;
cout<<"please type ur first guess"<<endl;
cin>>y;
while(y!=x)
{
if(y<=(x-10))
cout<<"Too low. Try again."<<endl;
else if(y<x)
cout<<"almost there."<<endl;
else if(y>=(x+10))
cout<<"too high. Try again."<<endl;
else if(y>10)
cout<<"almost there."<<endl;
cin>>y;
}
cout<<"excellent! you guessed the number!"<<endl;
cout<<"would you like to play again (y or n)?";
cin>>c;
}
return 0;
}

problem 6

#include<iostream>
#include<iomanip>
#include<cstdlib>
using namespace std;
int main()
{
int tim1=0,tim2=0,tim3=0,tim4=0,tim5=0,tim6=0,face;
for(int roll=1;roll<=6000;roll++)
{
face=1+rand()%6;
switch (face)
{
case 1:
++tim1;
break;
case 2:
++tim2;
break;
case 3:
++tim3;
break;
case 4:
++tim4;
break;
case 5:
++tim5;
break;
case 6:
++tim6;
break;
default :
cout<<"program should never get here!";
}
}
cout<<"Face"<<setw(15)<<"Times"
<<"\n  1"<<setw(15)<<tim1
<<"\n  2"<<setw(15)<<tim2
<<"\n  3"<<setw(15)<<tim3
<<"\n  4"<<setw(15)<<tim4
<<"\n  5"<<setw(15)<<tim5
<<"\n  6"<<setw(15)<<tim6<<endl;
return 0;
}

problem 11

#include<iostream>
using namespace std;
void draw_ESquare(int,char);
int main ()
{
int l;char c;
cout<<"enter the character the lenght of your square: \n";
cin>>c>>l;
cout<<endl;
draw_ESquare(l,c);

return 0;
}

void draw_ESquare(int l, char c)
{
for(int i=1;i<=l;i++)
{
for(int j=1;j<=l;j++)
if(i==1||j==l)
cout<<c;
else cout<<" ";
cout<<endl;
}
}

sorry i dont have the questions but if u read each example u will know what it do

these are the exercises that i found

i still have two e-books

c++ ill post them later by tonight

 bitarno 11-26-2008 06:04 PM

Re: C++ Help

Guys New Updates :

Arrays

Develop a C++ code where you declare an array of 50 elements. After assigning random values to your array, you need to compute the Mean, Median and Mode.
1.Mean
• Average (sum/number of elements)
2.Median
• Number in middle of sorted list
• 1, 2, 3, 4, 5 (3 is median)
• If even number of elements, take average of middle two
3.Mode
• Number that occurs most often
• 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)

2D Arrays

Exercise 1

Develop a C++ code where you declare a 2 dimensional array A of 50 rows and 30 columns.
1.Develop a function that assigns random values to your array. Each random value r should satisfy the following relation: -120 < r < 100 or r > 300.

2.Develop a function that generates a new array B in which you store the average of the elements stored over the same column in array A

Exercise 2

There are three students and there have been 4 quizzes. The results are
·first student got 77, 68, 86, 73
·second student got 96, 87, 89, 78
·third student got 70, 90, 86, 81

Goal
1.Develop a function that prints the grades of students
2.Develop a function that finds the highest grade
3.Develop a function that finds the lowest grade
4.Develop a function that computes the average of each student

Hint:
Use a multiple-subscripted array (table)
·Rows are students

Strings

EXERCISE 1
Develop a function that reads a string, removes all space from that string and prints out the result on the screen.
Example: read “hello there” and produce “hellothere”
• Redo the same thing for an array of characters.
EXERCISE 2
Develop a function that reads a string, replaces all letters “e” by “a” and prints out the result on the screen
Example: read “hello there” and produce “hallo thara”
• Redo the same thing for an array of characters.

EXERCISE 3
Develop a function that reads a string, finds the most repeated letter, and prints out the result on the screen.
Example: read “hello there” and produce “e” is the most repeated lettre
• Redo the same thing for an array of characters.

EXERCISE 4
Develop a function that reads a string and prints it backwards on the screen.
Example: read “hello there” and produce “ereht olleh”
• Redo the same thing for an array of characters.

EXERCISE 5
Write a myStrlen function which is the same as the library strlen function.
• Redo the same thing for an array of characters.

EXERCISE 6
Write the myStrcpy function which is the same as the library strcpy function.
• Redo the same thing for an array of characters.

EXERCISE 7
Write the myStrNcpy function which is the same as the library strncpy function.
• Redo the same thing for an array of characters.

EXERCISE 8
Write the myStrcmp function which is the same as the library strcmp function.
• Redo the same thing for an array of characters.

EXERCISE 9
Write the myStrNcmp function which is the same as the library strncmp function.
• Redo the same thing for an array of characters.
EXERCISE 10
Write a program that reads a line from the keyboard, and stores that line in a string.
a.Develop a function that allows you to insert a word wherever you want in that string.
b.Develop a function that allows you to delete any word of that string.
• Redo the same thing for an array of characters.

Thx Alot Guys ... Remember Visual C++ compiler :D

All times are GMT +1. The time now is 06:09 AM.