Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,739 members, 7,824,106 topics. Date: Friday, 10 May 2024 at 11:08 PM

A Thread For C++ Programs - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / A Thread For C++ Programs (991 Views)

A Thread On My Experiment With Reinforcement Learning & Artificial Intelligence. / A Thread For Tutorial On Python Programming / Whatsapp Group For C++ Beginners (2) (3) (4)

(1) (2) (Reply) (Go Down)

Re: A Thread For C++ Programs by Tim1212(m): 10:02am On Aug 20, 2020
#include <iostream>

using namespace std;

int main()
{
int score;

do
{
cout << "Enter a score between 0 and 50: ";
cin >> score;
cout << endl;
}
while (score < 0 || score > 50);

return 0;

}
Re: A Thread For C++ Programs by Tim1212(m): 10:03am On Aug 20, 2020
#include <iostream>

using namespace std;

int main()
{
int num, temp, sum;

cout << "Enter a positive integer: ";
cin >> num;
cout << endl;

temp = num;

sum = 0;

do
{
sum = sum + num % 10;

num = num / 10;

}
while (num > 0);

cout << "The sum of the digits = " << sum << endl;

if (sum % 9 == 0)
cout << temp << " is divisible by 3 and 9" << endl;
else if (sum % 3 == 0)
cout << temp << " is divisible by 3, and not 9 " << endl;
else
cout << temp << " is not divisible by 3 or 9" << endl;

return 0;
}
Re: A Thread For C++ Programs by Tim1212(m): 10:04am On Aug 20, 2020
#include <iostream>

using namespace std;

int main()
{
int sum = 0;
bool isNegative = false;
int num;
cout << "Enter sum of positive integers: ";

cin >> num;
cout << endl;


while (cin && !isNegative)
{
if (num < 0)
{
cout << "Negative number found in the data! " << endl;
isNegative = true;
}

else
{
sum = sum + num;
cin >> num;
}

}
cout << "The sum of number entered is: " << sum
<< endl;
return 0;
}
Re: A Thread For C++ Programs by Tim1212(m): 10:06am On Aug 20, 2020
#include <iostream>

using namespace std;

int main()
{
int sum = 0;
int num;

cout << "Enter the sum of positive number: ";
cin >> num;


while (cin)
{
if (num < 0)
{
cout << "Negative number is found in the data!" << endl;
break;
}



sum = sum + num;
cin >> num;


}




cout << "The sum of the number entered is: " << sum
<< endl;

return 0;
}
Re: A Thread For C++ Programs by Tim1212(m): 10:12am On Aug 20, 2020
#include <iostream>

using namespace std;

int main()
{
int nth;
int factNum;


cout << "Enter the factorial value a number: ";
cin >> factNum;
cout << endl;

for (nth = 1; factNum >= nth; nth++)

{

cout << (factNum - nth)
<< endl;


}


cout << " The value is = " << factNum * n
<< endl;

return 0;
}
Re: A Thread For C++ Programs by Tim1212(m): 10:14am On Aug 20, 2020
#include <iostream>

using namespace std;

int main()
{
int count;
int n;



cout << "Enter the factorial value ";
cin >> n;
cout << endl;

count = 2;

while (count <= n)

count = 1 * ++count;
cout << "The factorial value of " << n << " is "
<< count << endl;

return 0;
}
Re: A Thread For C++ Programs by Tim1212(m): 10:18am On Aug 20, 2020
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
string firstName;
string secondName;
double testScore;
double sum = 0;
char grade = ' ';
int count = 0;

ifstream inFile;
ofstream outFile;

inFile.open("test.txt"wink;

if (!inFile)
{
cout << "Cannot open input file. "
<< "program terminates!" << endl;
return 1;
}


outFile.open("outData.out"wink;

outFile << fixed << showpoint << setprecision(2);

inFile >> firstName >> secondName;
inFile >> testScore;

while (inFile)
{
sum = sum + testScore;
count++;

switch (static_cast<int> (testScore) / 10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
grade = 'F';
break;
case 6:
grade = 'D';
break;
case 7:
grade = 'C';
break;
case 8:
grade = 'B';
break;
case 9:
case 10:
grade = 'A';
break;
default:
cout << "invalid score." << endl;
}

outFile << left << setw(12) << firstName
<< setw(12) << secondName
<< right << setw(4) << testScore
<< setw(2) << grade << endl;

inFile >> firstName >> secondName;
inFile >> testScore;
}
outFile << endl;
if (count != 0)
outFile << "Class Average: " << sum / count
<< endl;
else
outFile << "No data. " << endl;

inFile.close();
outFile.close();

return 0;
}

input

Steve Gill 89
Rita Johnson 91.5
Randy Brown 85.5
Seema Arora 76.5
Samir Mann 73


Output

Steve Gill 89.00 B
Rita Johnson 91.50 A
Randy Brown 85.50 B
Seema Arora 76.50 C
Samir Mann 73.00 C

Class Average: 83.10
Re: A Thread For C++ Programs by Tim1212(m): 10:21am On Aug 20, 2020
#include <iostream>

using namespace std;

int main()
{
int f;
int c;

cout << "Enter temperature in Fahrenheit: ";
cin >> f;
cout << endl;

c = static_cast<int>( 5.0 / 9 * (f - 32));

cout << f << " degree F = " << c << " degree C. "
<< endl;

return 0;

}
Re: A Thread For C++ Programs by Tim1212(m): 10:21am On Aug 20, 2020
#include <iostream>
#include <iomanip>

using namespace std;



int main()

{
double Dt;
int a1, a2, a3, a4, a5, a6,
a7, a8, a9, a10, a11, a12,
a13, a14, a15, a16, a17, a18,
a19, a20, a21, a22, a23, a24,
a25, a26, a27, a28, a29, a30;



cout << fixed << showpoint << setprecision(2);

cout << "To determine the determinant of 5 x 6 matrix " << endl;



cout << "Enter the variables " << endl;
cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6;
cin >> a7 >> a8 >> a9 >> a10 >> a11 >> a12;
cin >> a13 >> a14 >> a15 >> a16 >> a17 >> a18;
cin >> a19 >> a20 >> a21 >> a22 >> a23 >> a24;
cin >> a25 >> a26 >> a27 >> a28 >> a29 >> a30;




Dt = ((a1) * (a8) * (a15) * ((a23) * (a30) - (a24) * (a29)) - ((a22) * (a30) - (a24) * (a28))
+ ((a22) * (a29) - (a23) * (a28)))

- ((a2) * (a7) * (a15) * ((a23) *(a30) - (a24) * (a29)) - ((a22) * (a30) - (a24) * (a28))
+ ((a22) * (a29) - (a23) * (a28)) )

+ ((a3) * (a7) * (a14) * ((a23) *(a30) - (a24) * (a29)) - ((a22) * (a30) - (a24) * (a28))
+ ((a22) * (a29) - (a23) * (a28)) )

- ((a4) * (a7) * (a14) * ((a21) * (a30) - (a24) * (a29)) - ((a21) * (a30) - (a24) * (a28))
+ ((a21) * (a29) - (a23) * (a27)))

+ ((a5) * (a7) * (a14) * ((a22) * (a30) - (a24) * (a28)) - ((a21) * (a30) - (a24) * (a27))
+ ((a21) * (a28) - (a22) * (a27)))

- ((a6) * (a7) * (a14) * ((a22) * (a29) - (a23) * (a27)) - ((a21) * (a29) - (a23) * (a26))
+ ((a21) * (a27) - (a22) * (a26)));

cout << "The determinant of the matrix is = " << Dt << endl;

return 0;
}
Re: A Thread For C++ Programs by Tim1212(m): 10:25am On Aug 20, 2020
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int factorial(int);



int main()

{

int f;
double x, sinX;
int n;

cout << "TIM1212" << endl;
cout << "16CS/0201/xx"<< endl;
cout << "POWER OPTION" << endl;
cout << endl;



cout << "Enter integer to find the factorial: ";

cin >> n;

if (n < 0)
cout << "Negative number found " << endl;
else
f = factorial(n);

cout << n << "! = " << f << endl;


cout <<" Enter the value of x, to determine the 5th terms of Mac-Laurin series: ";
cin >> x;
cout << endl;



sinX = x - pow(x, 3) / (factorial(3)) + pow(x, 5) / (factorial(5))
- pow(x, 7) / (factorial(7)) + pow(x, 9) / (factorial(9));



cout << "Sin(" << x << "wink = " << sinX << endl;


return 0;

}


int factorial(int n)

{
if (n == 0)

return 1;
else
return(n * factorial(n - 1));
}
Re: A Thread For C++ Programs by Tim1212(m): 10:25am On Aug 20, 2020
#include <iostream>
#include <iomanip>

using namespace std;

double add(double, double);
double mutiply(double, double);
double divide(double, double);
double subtract(double, double);

int main()
{
double a, b;

cout << fixed << showpoint << setprecision(2);

cout << "This program performs the basic arithmetic operation "
<<"\nsuch as:[addition, mutiplication, division and subtraction] " << endl << endl;

cout << "Enter the sum of two real number: ";
cin >> a >> b;
cout << endl;

cout << a << " + " << b << " = " << add(a, b)
<< endl;

cout << "Enter the product of two real number: ";
cin >> a >> b;
cout << endl;

cout << a << " * " << b << " = " << mutiply(a, b) << endl;


cout << "Enter the quotiant of two real number: ";
cin >> a >> b;
cout << endl;

cout << a << " / " << b << " = " << divide(a, b) << endl;

cout << "Enter the difference of two real number: ";
cin >> a >> b;
cout << endl;

cout << a << " - " << b << " = " << subtract(a, b) << endl;


return 0;
}

double add(double a, double b)
{

return (a + b );
}

double divide(double a, double b)
{
return(a / b);
}

double mutiply(double a, double b)
{
return((a) * (b));
}

double subtract(double a, double b)
{
return ((a) - (b));
}
Re: A Thread For C++ Programs by chrisUmendeche: 1:06pm On Aug 20, 2020
C++ can be applied to an embedded development - microcontrollers (Arduino, Raspberry Pi, etc.), internet of things (IoT), everything where performance matters. A lot of stuff in the automotive industry is also written in C++.

You must be good in data structure and program design

You can join our WhatsApp class for the training.
Re: A Thread For C++ Programs by chrisUmendeche: 1:08pm On Aug 20, 2020
You can pick any questions if you have any concern. We are able to explain it to your understanding.
Re: A Thread For C++ Programs by chrisUmendeche: 1:11pm On Aug 20, 2020
More practical questions are coming .......
Re: A Thread For C++ Programs by chrisUmendeche: 9:35pm On Aug 20, 2020
C++ can be applied virtually in all things
Re: A Thread For C++ Programs by Regex: 5:15pm On Aug 21, 2020
I'm more of a snake guy.
Re: A Thread For C++ Programs by JiKing(m): 9:00pm On Aug 21, 2020
Timi boss...do small explanation nah.
Re: A Thread For C++ Programs by Tim1212(m): 1:30am On Aug 22, 2020
JiKing:
Timi boss...do small explanation nah.

Pick anyone, then we start from there
Re: A Thread For C++ Programs by chukxy: 5:44am On Aug 22, 2020
@Tim1212, quick question for you. Why should we implement our own custom copy constructor in a class instead of using the default copy constructor in a class in C++. If you explain this with example, I will appreciate.

Secondly, why is the advantage of using heap memory over stack memory in c++? Again, I will appreciate if you explain with example.

Many thanks in anticipation.
Re: A Thread For C++ Programs by Nobody: 11:36am On Aug 22, 2020
chukxy:
@Tim1212, quick question for you. Why should we implement our own custom copy constructor in a class instead of using the default copy constructor in a class in C++. If you explain this with example, I will appreciate.

Secondly, why is the advantage of using heap memory over stack memory in c++? Again, I will appreciate if you explain with example.

Many thanks in anticipation.
Implement your own constructor when you want to do some certain action on creation of a new class, like a class LinkedList for example, when I create a new LinkedList class I would like to create a new node structure inside the class then set an internal node structure pointer variable called head to the node. It will be far easier to do this with a constructor than.

As for the second question, the heap is not used by functions but the stack is, allocating memory on the stack is platform specific (works differently on different CPUs) so doing that might cause issues that would annoy you, but allocating on the heap pretty much works the same way across different systems. To me allocating memory on the stack is much easier because if you lose the reference to that piece of memory there is nothing wrong because another function would just overwrite it, but I still use the heap for the sakes of portability.

1 Like

Re: A Thread For C++ Programs by chukxy: 1:20pm On Aug 22, 2020
SegFault:

Implement your own constructor when you want to do some certain action on creation of a new class, like a class LinkedList for example, when I create a new LinkedList class I would like to create a new node structure inside the class then set an internal node structure pointer variable called head to the node. It will be far easier to do this with a constructor than.

As for the second question, the heap is not used by functions but the stack is, allocating memory on the stack is platform specific (works differently on different CPUs) so doing that might cause issues that would annoy you, but allocating on the heap pretty much works the same way across different systems. To me allocating memory on the stack is much easier because if you lose the reference to that piece of memory there is nothing wrong because another function would just overwrite it, but I still use the heap for the sakes of portability.
Many thanks SegFault. Please would you mind using code examples in your explanation that will make it clearer?
Re: A Thread For C++ Programs by Bahat: 3:11pm On Aug 22, 2020
If you intend to go far with this cpp thing, you should pick a book on Microsoft cpp coding there you can be sure to write some good programs that run in windows.
Re: A Thread For C++ Programs by Bahat: 3:41pm On Aug 22, 2020
chukxy:
@Tim1212, quick question for you. Why should we implement our own custom copy constructor in a class instead of using the default copy constructor in a class in C++. If you explain this with example, I will appreciate.

Secondly, why is the advantage of using heap memory over stack memory in c++? Again, I will appreciate if you explain with example.

Many thanks in anticipation.

If memory serves me right. Heap memory are dynamic memory location allocated with malloc(), extendedby calloc() and freed with free(). Functions use stack memory to save function parameters and local variables.

You use stack memory location in your example codes to store variables declared in the body of your program.

Both stack and heap are used by functions. best to use stack space if you aren't doing something serious. heaps are used to manage resources. You can't allocate a specified memory to what you don't know the exact amount of resources coming through. Both stack and heap are guarded by compiler to guard against memory leakage.
There are good programming practise to using those memory spaces.
Re: A Thread For C++ Programs by chukxy: 7:25pm On Aug 22, 2020
Bahat:


If memory serves me right. Heap memory are dynamic memory location allocated with malloc(), extendedby calloc() and freed with free(). Functions use stack memory to save function parameters and local variables.

You use stack memory location in your example codes to store variables declared in the body of your program.

Both stack and heap are used by functions. best to use stack space if you aren't doing something serious. heaps are used to manage resources. You can't allocate a specified memory to what you don't know the exact amount of resources coming through. Both stack and heap are guarded by compiler to guard against memory leakage.
There are good programming practise to using those memory spaces.
Hi Bahat, in C++ , we use new keyword to create the heap memory and use delete to release it. Malloc() and calloc() are for C and we use free() to release it.

Concerning my question, an example explanation from experienced guys like you would help more in grasping the concept more. Not only for me but others who will read it.

Many thanks.
Re: A Thread For C++ Programs by Bahat: 8:47pm On Aug 22, 2020
chukxy:

Hi Bahat, in C++ , we use new keyword to create the heap memory and use delete to release it. Malloc() and calloc() are for C and we use free() to release it.

Concerning my question, an example explanation from experienced guys like you would help more in grasping the concept more. Not only for me but others who will read it.

Many thanks.

Oh yeah. I used to work with C not really a cpp fan because it's extended C. Lol
Re: A Thread For C++ Programs by Tim1212(m): 1:33pm On Aug 23, 2020
chukxy:
@Tim1212, quick question for you. Why should we implement our own custom copy constructor in a class instead of using the default copy constructor in a class in C++. If you explain this with example, I will appreciate.

Secondly, why is the advantage of using heap memory over stack memory in c++? Again, I will appreciate if you explain with example.

Many thanks in anticipation.

What you asked is beyond the scope of this thread, though you have been cleared by others.

Ask question pertaining to the C++ programs we have here.
Re: A Thread For C++ Programs by Tim1212(m): 6:19am On Aug 24, 2020
Tim1212:
#include <iostream>

using namespace std;

int main ()
{
int length;
int width;
double perimeter;
double area;

length = 6;
width = 4;
perimeter = 2 * ( length + width );
area = length * width;

cout << "Length = " << length << endl;
cout << "width = " << width << endl;
cout << "Area = " << area << endl;
cout << " Perimeter = " << perimeter << endl;

return 0;
}

I will be explaining every single program starting from the beginning
Re: A Thread For C++ Programs by Tim1212(m): 3:20pm On Aug 25, 2020
Tim1212:
#include <iostream>

using namespace std;

int main()
{
int f;
int c;

cout << "Enter temperature in Fahrenheit: ";
cin >> f;
cout << endl;

c = static_cast<int>( 5.0 / 9 * (f - 32));

cout << f << " degree F = " << c << " degree C. "
<< endl;

return 0;

}


This program is applied in thermometer

(1) (2) (Reply)

Where Can I Learn IT In Lagos / Wordpress Wahala Please Help / Programmers Is This True?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 60
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.