0% found this document useful (0 votes)
2 views

cpp-advanced

The document contains multiple C++ code examples demonstrating various programming concepts such as the usage of the 'goto' and 'continue' keywords, array manipulation, matrix operations, and function definitions. Each section includes code that reads user input, performs calculations, and outputs results, covering topics like summing arrays, finding squares, and matrix multiplication. Overall, it serves as a practical guide for beginners to understand basic programming constructs in C++.

Uploaded by

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

cpp-advanced

The document contains multiple C++ code examples demonstrating various programming concepts such as the usage of the 'goto' and 'continue' keywords, array manipulation, matrix operations, and function definitions. Each section includes code that reads user input, performs calculations, and outputs results, covering topics like summing arrays, finding squares, and matrix multiplication. Overall, it serves as a practical guide for beginners to understand basic programming constructs in C++.

Uploaded by

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

// 31.

Usage of goto keyword


#include <iostream>
using namespace std;

int main() {
int i = 1;

start:
cout << i << " ";
i++;

if (i <= 10) {
goto start; // jumps to the start label
}

cout << endl << "End of goto example" << endl;


return 0;
}

// 32. Usage of continue keyword


#include <iostream>
using namespace std;

int main() {
cout << "Printing odd numbers between 1 and 10:" << endl;

for (int i = 1; i <= 10; i++) {


if (i % 2 == 0) {
continue; // Skip even numbers
}
cout << i << " ";
}

cout << endl;


return 0;
}

// 33. Create a single dimensional array read 10 numbers and print the same
#include <iostream>
using namespace std;

int main() {
int numbers[10];

cout << "Enter 10 integers:" << endl;

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

cout << "The numbers you entered are:" << endl;

// Print the numbers


for (int i = 0; i < 10; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}

// 34. Create 2 dimensional array add the rows and columns


#include <iostream>
using namespace std;

int main() {
int rows = 3;
int cols = 3;
int matrix[3][3];
int rowSum[3] = {0};
int colSum[3] = {0};

cout << "Enter the elements of a 3x3 matrix:" << endl;

// Input matrix elements


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Enter element at position [" << i << "][" << j << "]: ";
cin >> matrix[i][j];
}
}

// Calculate sum of rows and columns


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
rowSum[i] += matrix[i][j];
colSum[j] += matrix[i][j];
}
}

// Display matrix
cout << "Matrix:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << "\t";
}
cout << endl;
}

// Display row sums


cout << "Row sums:" << endl;
for (int i = 0; i < rows; i++) {
cout << "Row " << i << ": " << rowSum[i] << endl;
}

// Display column sums


cout << "Column sums:" << endl;
for (int j = 0; j < cols; j++) {
cout << "Column " << j << ": " << colSum[j] << endl;
}

return 0;
}

// 35. Find the square of each element of the array


#include <iostream>
using namespace std;

int main() {
int size = 5;
int numbers[5];
int squares[5];

cout << "Enter 5 integers:" << endl;

// Read numbers
for (int i = 0; i < size; i++) {
cout << "Enter number " << (i+1) << ": ";
cin >> numbers[i];

// Calculate square
squares[i] = numbers[i] * numbers[i];
}

// Display original numbers and their squares


cout << "Number\tSquare" << endl;
for (int i = 0; i < size; i++) {
cout << numbers[i] << "\t" << squares[i] << endl;
}

return 0;
}

// 36. Find the sum of the array


#include <iostream>
using namespace std;

int main() {
int size = 5;
int numbers[5];
int sum = 0;

cout << "Enter 5 integers:" << endl;

// Read numbers and calculate sum


for (int i = 0; i < size; i++) {
cout << "Enter number " << (i+1) << ": ";
cin >> numbers[i];
sum += numbers[i];
}

cout << "The sum of the array elements is: " << sum << endl;

return 0;
}

// 37. Matrix multiplication - multi dimensional array


#include <iostream>
using namespace std;

int main() {
int a[3][3], b[3][3], result[3][3];

cout << "Enter elements of first 3x3 matrix:" << endl;


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter element a[" << i << "][" << j << "]: ";
cin >> a[i][j];
}
}

cout << "Enter elements of second 3x3 matrix:" << endl;


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter element b[" << i << "][" << j << "]: ";
cin >> b[i][j];
}
}

// Initialize result matrix with zeros


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = 0;
}
}

// Multiply matrices
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}

// Display the result


cout << "Result of matrix multiplication:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << result[i][j] << "\t";
}
cout << endl;
}

return 0;
}

// 38. Simple function


#include <iostream>
using namespace std;

// Function declaration
void greet();

int main() {
// Function call
greet();
return 0;
}

// Function definition
void greet() {
cout << "Hello from a simple function!" << endl;
}
// 39. Function with parameter
#include <iostream>
using namespace std;

// Function declaration
void greetPerson(string name);

int main() {
string userName;

cout << "Enter your name: ";


getline(cin, userName);

// Function call with parameter


greetPerson(userName);

return 0;
}

// Function definition
void greetPerson(string name) {
cout << "Hello, " << name << "! Welcome to C++ Programming." << endl;
}

// 40. Return value from function


#include <iostream>
using namespace std;

// Function declaration
int add(int a, int b);

int main() {
int num1, num2;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

// Function call with return value


int sum = add(num1, num2);

cout << "Sum: " << sum << endl;

return 0;
}

// Function definition with return value


int add(int a, int b) {
return a + b;
}

You might also like