Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Coding Examples

Uploaded by

Oula Abd Al-moty
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Coding Examples

Uploaded by

Oula Abd Al-moty
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q2: JavaScript Function for Course Registration

Here's the JavaScript function `courseReg` that gathers basic information from a student,
registers courses, and displays the data:

function courseReg() {
// Gather Basic Information
let studentName = prompt("Enter your name:");
let studentNumber = prompt("Enter your student number:");
let courseCount = parseInt(prompt("How many courses do you want to register for? (Up
to 5)"));

// Validate course count input


if (isNaN(courseCount) || courseCount < 1 || courseCount > 5) {
alert("Please enter a number between 1 and 5.");
return;
}

// Register Courses
let courses = [];
for (let i = 0; i < courseCount; i++) {
let courseCode = prompt(`Enter course code for course ${i + 1}:`);
courses.push(courseCode);
}

// Display Information
document.getElementById("studentname").textContent = studentName;
document.getElementById("studentnumber").textContent = studentNumber;

// Display the list of registered courses


let courseListElement = document.getElementById("courselist");
courseListElement.innerHTML = ""; // Clear previous course list
courses.forEach(course => {
let listItem = document.createElement("li");
listItem.textContent = course;
courseListElement.appendChild(listItem);
});
}
The corresponding HTML for this function:

<h1>Course Registration</h1>
<p><strong>Student Name:</strong> <span id="studentname"></span></p>
<p><strong>Student Number:</strong> <span id="studentnumber"></span></p>
<p><strong>Registered Courses:</strong></p>
<ul id="courselist"></ul>
<button onclick="courseReg()">Register for Courses</button>

Q3: C++ Program for Function Overloading with Templates

Here's a C++ program that uses function overloading and templates to calculate the
perimeter of a rectangle or square:

#include <iostream>
using namespace std;

// Template function for calculating the perimeter


template <typename T>
T calculatePerimeter(T side) {
return 4 * side; // Perimeter of a square
}

template <typename T>


T calculatePerimeter(T length, T width) {
return 2 * (length + width); // Perimeter of a rectangle
}

int main() {
cout << "Perimeter of square with side 5: " << calculatePerimeter(5) << endl;
cout << "Perimeter of rectangle with length 5 and width 3: " << calculatePerimeter(5, 3)
<< endl;
cout << "Perimeter of rectangle with length 5.5 and width 3.2: " <<
calculatePerimeter(5.5, 3.2) << endl;

return 0;
}
Q1: C++ Program for Overloaded Function `display`

Here's a C++ program that includes overloaded functions for displaying different numerical
data types:

#include <iostream>
using namespace std;

// First version: Accepts an integer and a double


void display(int num, double numDouble) {
cout << "Integer: " << num << ", Double: " << numDouble << endl;
}

// Second version: Accepts a double


void display(double num) {
cout << "Double number: " << num << endl;
}

// Third version: Accepts an integer


void display(int num) {
cout << "Integer number: " << num << endl;
}

int main() {
int intNum = 5;
double doubleNum = 3.14;

// Calling the different versions of display


display(intNum, doubleNum);
display(doubleNum);
display(intNum);

return 0;
}

You might also like