0% found this document useful (0 votes)
2 views26 pages

ChatGPT Reference

Hintai

Uploaded by

ksmk123987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views26 pages

ChatGPT Reference

Hintai

Uploaded by

ksmk123987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

ChatGPT

Questions and Codes for Each Lab Exercise

---

LAB 1: POINTERS

1. Question:
Create a function to calculate the logarithm (base-
10) of a user input number using the pass-by-
reference method. Display the original value and
the result.

Code (log_bais_10.cpp):

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

void calculateLog10(double
&num) { double original = num;
num = log10(num);
cout << "Original value: " << original <<
endl; cout << "Logarithm base-10: " <<
num << endl;
}

int main() {
double
number;
cout << "Enter a
number: "; cin >>
number;
calculateLog10(number);
return 0;
}

2. Question:
Create an array of 5 elements and display the
addresses of all elements.

Code (array_address.cpp):

1/19
#include
<iostream> using
namespace std;

int main()
{ int
arr[5];
cout << "Enter 5 elements:" <<
endl; for (int i = 0; i < 5; i++) {
cin >> arr[i];
cout << "Address of element " << i + 1 << ": " <<
&arr[i]
<< endl;
}
return 0;
}

---

LAB 2: C++ ARRAYS

1. Question:
Display the sum and average of array elements
using a loop.

Code (sum_ave.cpp):

#include
<iostream> using
namespace std;

int main() {
int arr[5], sum = 0;
cout << "Enter 5
numbers: "; for (int i = 0;
i < 5; i++) {
cin >> arr[i];
sum +=
arr[i];
}
double average = sum /
5.0; cout << "Sum: " <<
sum << endl;
cout << "Average: " << average
<< endl; return 0;

2/19
}

2. Question:
Use an array to allow the user to print their

name. Code (name_array.cpp):

#include
<iostream> using
namespace std;

int main() {
char name[50];
cout << "Enter your name: ";
cin.getline(name, 50);
cout << "Your name is: " << name <<
endl; return 0;
}

---

LAB 3: C++ STACK

1. Question:
Create a stack of size 10, ask the user to push the
elements, pop twice, and then print the content of
the stack.

Code (in10_out8.cpp):

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

int main() {
stack<int>
st; int
value;
cout << "Enter 10 elements to push onto the
stack:" << endl;

3/19
for (int i = 0; i < 10; i+
+) { cin >> value;
st.push(value);
}

// Pop two
elements
st.pop();
st.pop();

// Print stack content


cout << "Stack content after popping 2
elements:" << endl;
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
cout <<
endl;
return 0;
}

2. Question:
Modify a program that asks a user to enter
numbers, then display only even numbers.

Code (even):

#include
<iostream> using
namespace std;

int main() {
int numbers[10];
cout << "Enter 10
numbers: "; for (int i = 0;
i < 10; i++) {
cin >> numbers[i];
}

cout << "Even numbers


are: "; for (int i = 0; i < 10;
i++) {
if (numbers[i] % 2 == 0)
{ cout << numbers[i]
<< " ";
}

4/19
}
cout <<
endl;
} return 0;

--
-

LAB 4: C++ QUEUE

1. Question:
Allow the user to enter 5 elements, then calculate
the average using a queue.

Code (avrage.cpp):

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

int main() {
queue<int> q;
int value, sum = 0;

cout << "Enter 5 numbers:"


<< endl; for (int i = 0; i < 5;
i++) {
cin >> value;
q.push(value);
sum += value;
}

---

}
double average = sum / 5.0; "Average: " << average << endl;
cout << return 0;

5/19
.‫ إذا‬S ü L› ‫ إ أي‬d z2L‫ ة إ‬L ± ‫ أو‬izaï ، ‫ أ‬± i

p ‫ا‬z ً ‫ا‬AD z
ً ±‫و‬ D Lً a ‫و‬z±i ‫د‬gS ‫ ا‬. D‫ا‬ ± ‫وا‬ i I‫ها‬ S‫ر‬I2 D L ‫ ﷲ‬z±2 ‫ ًا‬S
6A LAD p± D
‫ إ‬±gï y ‫إ‬ ›S ü L ‫ إذا‬C++. ± ‫ا‬gb ‫ وا‬،‫ت‬zSa L ‫ ا‬،‫ت‬gaAa L ‫ ا‬،‫ات‬ ga ‫ا‬
،‫ء‬j ‫ي‬L i
ï ،‫ت‬Lı±b ‫ ا‬±gï y ‫ء أو‬i Lb ‫ا‬ D LAD ‫ة‬ziz ‫ات‬L j±D ‫ أو إ‬،‫د‬gS ‫ ا‬±¿ ï›
ựIb. ‫ا‬ ‫دد‬

char_int.cpp
File

‫ ال‬٣g a ‫أ‬stack ‫ال‬g ‫ ا‬yi "Write a code that return true if the user wr
ä a ‫ ا‬aIa ‫¿ ا ا‬d L L ‫وا‬

LAB 3: C++ STACK

3. Question:

Write a code that returns true if the user writes a character and false if they
write an integer.

Code (char_int.cpp):

cpp

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

int main()
{
stack<char>
stk; char input;
cout << "Enter a character or a
number: "; cin >> input;
stk.push(input);
char top = stk.top();

if (!stk.empty())
{
if (top >= '0' && top <= '9')
{
cout << top << " is a number." << endl;
}

6/19
else
{

cout << top << " is a character." << endl;


}
}

return 0;
}

‫ة‬Li L aI z2L‫ أ‬،‫د‬Aï ± gSI ‫أو‬ ± L ‫ ت إ‬izAï ‫أي‬ ‫إ‬ ›S ü L ‫إذا‬.

‫ ا‬L‫ن أ‬S ự S ‫ ا‬L I L ‫وا‬z2‫ ة دون‬dzäd

LAB 1: POINTERS

1. Question:
Create a function to calculate the logarithm (base-10) of a user input
number using the pass-by-reference method. Display the original value and
the result.

cpp

#include <iostream> #include <cmath>


using namespace std;

void calculateLog10(double &num) { double original = num;


num = log10(num);
cout << "Original value: " << original << endl; cout << "Logarithm base-10: " << num << e
}

int main() {
double number;
cout << "Enter a number: "; cin >> number; calculateLog10(number); return 0;
}

7/19
2. Question:
Create an array of 5 elements and display the addresses of all elements.

cpp

#include <iostream>
using namespace std;

int main() {
int arr[5];
cout << "Enter 5 elements:" << endl; for (int i = 0; i < 5; i++) {
cin >> arr[i];
cout << "Address of element " << i + 1 << ": " << &arr[i] << endl;
}
return 0;
}

LAB 2: C++ ARRAYS

1. Question:
Display the sum and average of array elements using a loop.

cpp

#include <iostream>
using namespace std;

int main() {
int arr[5], sum = 0;
cout << "Enter 5 numbers: "; for (int i = 0; i < 5; i++) {
cin >> arr[i]; sum += arr[i];
}
double average = sum / 5.0; cout << "Sum: " << sum << endl;
cout << "Average: " << average << endl;

8/19
return 0;
}

2. Question:
Use an array to allow the user to print their name.

cpp

#include <iostream>
using namespace std;

int main() {
char name[50];
cout << "Enter your name: "; cin.getline(name, 50);
cout << "Your name is: " << name << endl; return 0;
}

LAB 3: C++ STACK

1. Question:
Create a stack of size 10, ask the user to push the elements, pop twice,
and then print the content of the stack.

cpp

#include <iostream> #include <stack>


using namespace std;

int main() {
stack<int> st; int value;

cout << "Enter 10 elements to push onto the stack:" << endl; for (int i = 0; i < 10; i++) {
cin >> value;

9/19
st.push(value);
}

// Pop two elements st.pop();


st.pop();

// Print stack content


cout << "Stack content after popping 2 elements:" << endl; while (!st.empty()) {
cout << st.top() << " "; st.pop();
}
cout << endl; return 0;

2. Question:
Modify a program that asks a user to enter numbers, then display only even
numbers.

cpp

#include <iostream>
using namespace std;

int main() {
int numbers[10];
cout << "Enter 10 numbers: "; for (int i = 0; i < 10; i++) {
cin >> numbers[i];
}

cout << "Even numbers are: "; for (int i = 0; i < 10; i++) {
if (numbers[i] % 2 == 0) { cout << numbers[i] << " ";
}
}
cout << endl; return 0;

10/19
3. Question:
Write a code that returns true if the user writes a character and false if
they write an integer.

cpp

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

int main()
{
stack<char>
stk; char input;
cout << "Enter a character or a
number: "; cin >> input;
stk.push(input);
char top = stk.top();

if (!stk.empty())
{
if (top >= '0' && top <= '9')
{
cout << top << " is a number." << endl;
}
els
e
{
cout << top << " is a character." << endl;

}
}

return 0;
}

LAB 4: C++ QUEUE

1. Question:
Allow the user to enter 5 elements, then calculate the average using a queue.
11/19
cpp

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

int main() {
queue<int> q;
int value, sum = 0;

cout << "Enter 5 numbers:" <<


endl; for (int i = 0; i < 5; i+
+) {
cin >> value;
q.push(value)
; sum +=
value;
}

double average = sum / 5.0;


cout << "Average: " << average
<< endl; return 0;
}

V1.1 .cpp
File

‫ال‬ iL ‫ا د‬gS ‫ا‬ I2 ‫ل‬z2stack Ä ‫ ا‬aIa L ‫د‬gS ‫ا‬


‫ن‬gS±
LAB 3: C++ STACK
2. Modified Question:
Modify a program that asks a user to enter numbers, then display only
cpp
even numbers (updated version).
#include <iostream> #include <stack>
Updated Code:
using namespace std;

int main() {
stack<int> evens;

12/19
cout << "How many numbers do you want to
enter: "; int count;
cin >> count;
int evenCount = 0;

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


cout << "Enter number " << i + 1
<< ": "; int num;
cin >> num;
if (num % 2 ==
0) {
evens.push(num
);
evenCount++;
}
}

cout << "\n\n\n";


if (evenCount == 0) {
cout << "There are no even numbers." << endl;
} else if (evenCount == 1) {
cout << "The even number is: ";
} else {
cout << "The even numbers are:\n ";
}

while (!evens.empty()) {
cout << evens.top() <<
"\t"; evens.pop();
}

return 0;
}

ựIb ‫ا‬ ‫ دد‬L ï ،‫ح‬ ‫أو‬ ± L ‫ ت إ‬izAï ‫إ‬ ›S ü L ‫إذا‬.


‫ ا‬L‫ن أ‬S ự ‫ ا‬gSL‫ اد وا‬L I dLS z‫ ون‬dzäd

LAB 1: POINTERS

1. Question:
Create a function to calculate the logarithm (base-10) of a user input
number using the pass-by-reference method. Display the original value and
the result. 13/19
cpp

#include <iostream> #include <cmath>


using namespace std;

void calculateLog10(double &num) { double original = num;


num = log10(num);
cout << "Original value: " << original << endl; cout << "Logarithm base-10: " << num << e
}

int main() {
double number;
cout << "Enter a number: "; cin >> number; calculateLog10(number); return 0;
}

2. Question:
Create an array of 5 elements and display the addresses of all elements.

cpp

#include <iostream>
using namespace std;

int main() {
int arr[5];
cout << "Enter 5 elements:" << endl; for (int i = 0; i < 5; i++) {
cin >> arr[i];
cout << "Address of element " << i + 1 << ": " << &arr[i] << endl;
}
return 0;
}

14/19
LAB 2: C++ ARRAYS

1. Question:
Display the sum and average of array elements using a loop.

cpp

#include <iostream>
using namespace std;

int main() {
int arr[5], sum = 0;
cout << "Enter 5 numbers: "; for (int i = 0; i < 5; i++) {
cin >> arr[i]; sum += arr[i];
}
double average = sum / 5.0; cout << "Sum: " << sum << endl;
cout << "Average: " << average << endl; return 0;
}

2. Question:
Use an array to allow the user to print their name.

cpp

#include <iostream>
using namespace std;

int main() {
char name[50];
cout << "Enter your name: "; cin.getline(name, 50);
cout << "Your name is: " << name << endl; return 0;
}

15/19
LAB 3: C++ STACK

1. Question:
Create a stack of size 10, ask the user to push the elements, pop twice,
and then print the content of the stack.

cpp

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

int main() {
stack<int>
st; int
value;

cout << "Enter 10 elements to push onto the stack:"


<< endl; for (int i = 0; i < 10; i++) {
cin >>
value;
st.push(valu
e);
}

// Pop two
elements st.pop();
st.pop();

// Print stack content


cout << "Stack content after popping 2 elements:"
<< endl; while (!st.empty()) {
cout << st.top() <<
" "; st.pop();
}
cout <<
endl; return
0;
cpp
}

2. Question:
Modify a program that asks a user to enter numbers, then display only even
numbers. 16/19
#include
<iostream>
#include <stack>
using namespace
std;

int main() {
stack<int> evens;
cout << "How many numbers do you want to
enter: "; int count;
cin >> count;
int evenCount = 0;

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


cout << "Enter number " << i + 1
<< ": "; int num;
cin >> num;
if (num % 2 ==
0) {
evens.push(num
);
evenCount++;
}
}

cout << "\n\n\n";


if (evenCount == 0) {
cout << "There are no even numbers." << endl;
} else if (evenCount == 1) {
cout << "The even number is: ";
} else {
cout << "The even numbers are:\n ";
}

while (!evens.empty()) {
cout << evens.top() <<
"\t"; evens.pop();
}

return 0;
}

3. Question:
Write a code that returns true if the user writes a character and false if 17/19
they write an integer.
cpp

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

int main()
{
stack<char>
stk; char input;
cout << "Enter a character or a
number: "; cin >> input;
stk.push(input);
char top = stk.top();

if (!stk.empty())
{
if (top >= '0' && top <= '9')
{
cout << top << " is a number." << endl;
}
els
e
{
cout << top << " is a character." << endl;

}
}

return 0;
}

LAB 4: C++ QUEUE

1. Question:
cpp
Allow the user to enter 5 elements, then calculate the average using a queue.
#include <iostream>
#include <queue>

18/19
using namespace std;

int main() {
queue<int> q;
int value, sum = 0;

cout << "Enter 5 numbers:" << endl; for (int i = 0; i < 5; i++) {
cin >> value; q.push(value); sum += value;
}

double average = sum / 5.0;


cout << "Average: " << average << endl; return 0;

19/19

You might also like