Q2. Arrays, Strings, Pointers, Constants & References in C++
Question:
Write program to demonstrate use of arrays, strings, pointers, constants, and references.
My Approach
C++ me arrays, strings, pointers, constants aur references ka use daily programming tasks me hota hai.
Maine is program ke liye ek student marks management system ka scenario liya hai, jisme:
- Array → multiple subject marks store karne ke liye.
- String → student ka naam store karne ke liye.
- Pointer → memory me data access karne ke liye.
- Constant → subjects ka fixed count dikhane ke liye.
- Reference → total marks ko function ke through calculate karke return karne ke liye.
Program Code
File Name: array_pointer_demo.cpp
#include <iostream>
#include <string>
using namespace std;
// Function to calculate total using reference
void calculateTotal(const int marks[], int size, int &total) {
total = 0;
for (int i = 0; i < size; i++) {
total += marks[i];
}
}
int main() {
const int SUBJECTS = 5; // Constant value (fixed subjects)
string name;
cout << "Enter student name: ";
getline(cin, name); // Using string input
int marks[SUBJECTS]; // Array for marks
cout << "Enter marks for " << SUBJECTS << " subjects: " << endl;
for (int i = 0; i < SUBJECTS; i++) {
cout << "Subject " << (i + 1) << ": ";
cin >> marks[i];
}
// Using pointer to display marks
int *ptr = marks;
cout << "\nMarks entered (via pointer): ";
for (int i = 0; i < SUBJECTS; i++) {
cout << *(ptr + i) << " ";
}
cout << endl;
// Using reference to get total
int totalMarks;
calculateTotal(marks, SUBJECTS, totalMarks);
cout << "\nStudent Name: " << name << endl;
cout << "Total Marks: " << totalMarks << endl;
cout << "Percentage: " << (totalMarks / (float)SUBJECTS) << "%" << endl;
return 0;
}Sample Input / Output
Input:
Enter student name: Intesab Ansari
Enter marks for 5 subjects:
Subject 1: 80
Subject 2: 75
Subject 3: 90
Subject 4: 85
Subject 5: 88Output:
Marks entered (via pointer): 80 75 90 85 88
Student Name: Intesab Ansari
Total Marks: 418
Percentage: 83.6%Explanation
- Array → Marks of 5 subjects ko ek hi array me store kiya gaya.
- String → Student name ko store karne ke liye
stringclass ka use kiya. - Pointer →
ptrke through marks array ke elements ko access kiya. - Constant →
SUBJECTS = 5fix rakha gaya (constant). - Reference →
calculateTotal()function me total ko reference parameter ke through modify kiya gaya.
Real Time Relevance
- Array: Student record, employee salaries, product prices, etc.
- Strings: User names, emails, or any text-based input.
- Pointers: Dynamic memory allocation (e.g., database rows in memory).
- Constants: Fixed values like GST %, subjects count, or max login attempts.
- References: Functions ke andar directly values update karne ke liye.
Conclusion
Is program me humne ek student marks system banakar arrays, strings, pointers, constants aur references ko practically demonstrate kiya.
Real life scenario ke sath samajhna easy ho jata hai aur mujhe personally ye topic helpful laga jab maine C++ me mini-projects banaye the.
👤 Submitted by:
Intesab Ansari
MCA Student, NMU University
Portfolio: intesab.live
Last updated on