Having a terrible time with C++ programming & haven\'t been able to find any rea
ID: 3763550 • Letter: H
Question
Having a terrible time with C++ programming & haven't been able to find any real help. Can someone PLEASE provide a pdf or just display a range of C++ programs that I can follow & practice with? Especially programs that involve multiple operations at once (finding the average & difference of values), single operations, compounding interest, passing to functions, void functions, arrays, pass by reference, arrays, any of which that involve floating point values, calling & rewriting functions, inputting sentences, character string, etc... Clearly I don't blame you for not helping & don't expect a guide for everything unfortunately but the littllest help counts. I really need complete programs to follow & use, I've only had pieces of everything it doesn't help.
Explanation / Answer
// These are the basic programs If you will go through all these then you will be able to understand basics of C++. If you need more help then you can comment.
C++ program to calculate sum and average
#include<iostream.h>
#include<conio.h>
void main()
{
float a,b,c,sum,average;
cout<<“Enter three numbers:”;
cin>>a>>b>>c;
sum=a+b+c;
average=sum/3;
cout<<“SUM =”<<sum;
cout<<“Average =”<<average;
getch();
}
Program to calculate Factorial using reccursion
#include<iostream> //header file
using namespace std;
int main()
{
long long n,fact=1; // long long datatype to store larger values
cout<<" Enter Number: ";
cin>>n;
for(int i=1;i<=n;i++)
{ fact=fact*i;
} cout<<"Factorial of the number is : "<<fact<<endl;
return 0;
}
Compound Interest
#include<iostream.h>
#include<math.h>
void main()
{
float p,r,t,c;
clrscr();cout<<“Enter Principle,Rate and Time “;
cin>>p>>r>>t;
c=pow(p*(1+r/100),t);
cout<<“n”<<“Compound Interest = “<<c<<“%”;
}
Swap 2 numbers using call by reference
#include <iostream>
using namespace std;
void swap (int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main(int argc, char *argv[])
{
int i=5,j=10;
cout<<"Before swapping I = "<<i<<" J = "<<j<<endl;
swap(&i,&j);
cout<<"After swapping I = "<<i<<" J = "<<j<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Program using arrays
#include <iostream>
using namespace std;
int main() {
int n[5];
cout<<"Enter 5 numbers: ";
// Storing 5 number entered by user in an array using for loop. */
for (int i = 0; i < 5; ++i) {
cin>>n[i];
}
cout<<"First number: "<<n[0]<<endl; // first element of an array is n[0]
cout<<"Last number: "<<n[4]; // last element of an array is n[SIZE_OF_ARRAY - 1]
return 0;
}
Program for function overloading using floating points values.
#include <iostream>
using namespace std;
/* Function arguments are of different data type */
long add(long, long);
float add(float, float);
int main()
{
long a, b, x;
float c, d, y;
cout << "Enter two integers ";
cin >> a >> b;
x = add(a, b);
cout << "Sum of integers: " << x << endl;
cout << "Enter two floating point numbers ";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
return 0;
}
long add(long x, long y)
{
long sum;
sum = x + y;
return sum;
}
float add(float x, float y)
{
float sum;
sum = x + y;
return sum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.