0% found this document useful (0 votes)
0 views9 pages

Programming Solution

For computer science students that use c++

Uploaded by

metsnib
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)
0 views9 pages

Programming Solution

For computer science students that use c++

Uploaded by

metsnib
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/ 9

1.

#include <iostream>

Using namespace std;

Int main() {

Float numbers[10];

Cout << “Enter 10 real numbers:” << endl;

For(int i = 0; i < 10; i++) {

Cin >> numbers[i];

Cout << “The sixth element is: “ << numbers[5] << endl;

Return 0;

#include <iostream>

using namespace std;

int main() {

int matrix[4][3];

cout << "Enter 12 different values for the 4x3 matrix:" << endl;

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

for(int j = 0; j < 3; j++) {

cin >> matrix[i][j];

}
// Ninth element (0-based index 8) - row 2, column 2

cout << "The ninth element is: " << matrix[2][2] << endl;

return 0;

Question 2

**Output:** `The sum is24`

**Explanation:**

- The for loop has an empty body (notice the semicolon after the loop
condition)

- The loop runs while x (12) is less than i*2 (14), incrementing x each time

- x starts at 12, increments to 13 (still <14), then to 14 (not <14), so loop


stops with x=14

- After the loop, sum += x*2 means sum = 0 + 14*2 = 28

- However, there seems to be a typo in the output you provided – it should


be 28, not 24

## Question 3

**Output:** The program calculates and displays the sum of the digits of
the number entered by the user.

**Explanation:**

- The program reads an integer `n` from the user

- The while loop continues as long as `n` is greater than 0

- In each iteration:

- `x = n % 10` gets the last digit of `n`

- `a = a + x` adds that digit to the running total `a`

- `n = n / 10` removes the last digit from `n`


- When the loop ends, `a` contains the sum of all digits

- The program is correct and works as intended

## Question 4

**Output:** `-4 6 8 12 65`

**Explanation:**

- The code implements the Bubble Sort algorithm to sort the array in
ascending order

- `n` is calculated as the length of the array (5 elements)

- The nested loops compare adjacent elements and swap them if they’re
in the wrong order

- After sorting, the array is printed in order: -4, 6, 8, 12, 65

- The code is correct and works as intended

#include <iostream>

Using namespace std;

Int main() {

Int list[] = {65, 6, -4, 12, 8};

Int n = sizeof(list) / sizeof(list[0]);

Bool swapped = true;

Int i = 0;

While (swapped && i < n – 1) {

Swapped = false;

Int j = 0;

While (j < n – i – 1) {

If (list[j] > list[j + 1]) {


Int x = list[j + 1];

List[j + 1] = list[j];

List[j] = x;

Swapped = true;

J++;

I++;

For (i = 0; i < n; i++)

Cout << list[i] << “ “;

Return 0;

#include <iostream>

Using namespace std;

Int main() {

Char input;

Do {

Int numbers[5];

Cout << “Enter 5 numbers: “;

For(int i = 0; i < 5; i++) {

Cin >> numbers[i];

Int max = numbers[0];


For(int i = 1; i < 5; i++) {

If(numbers[i] > max) {

Max = numbers[i];

Cout << “The maximum number is: “ << max << endl;

Cout << “Press X to exit or any other key to continue: “;

Cin >> input;

} while(toupper(input) != ‘X’);

Return 0;

1.5 2.25 3.75

[Error] assignment of arrays is not allowed in C++

*Explanation:**

- The first line correctly prints the contents of array `a`

- The line `a = b;` attempts to assign one array to another, which is not
allowed in C++

- To fix this, you would need to copy elements individually or use


`memcpy`

#include <iostream>

#include <cstring>

Using namespace std;

Int main() {

Char names[3][50];
Cout << “Enter first names of 3 users:” << endl;

For(int i = 0; i < 3; i++) {

Cin >> names[i];

// Bubble sort using strcmp

For(int i = 0; i < 2; i++) {

For(int j = 0; j < 2 – i; j++) {

If(strcmp(names[j], names[j+1]) > 0) {

Char temp[50];

Strcpy(temp, names[j]);

Strcpy(names[j], names[j+1]);

Strcpy(names[j+1], temp);

Cout << “Names in alphabetical order:” << endl;

For(int i = 0; i < 3; i++) {

Cout << names[i] << endl;

Return 0;

0x6dfe60 (address of the array)

0x6dfe68 (address of the second row)

**Explanation:**
- `cout << a << endl;` prints the address of the first element of the array

- `cout << a[1] << endl;` prints the address of the second row (1.5, 2.25
is first row, 3.75, 5.6 is second row)

- The code is correct and works as intended

### Part b

The address of the fourth element is `0x6dfe70`.

**Calculation:**

- First element: 0x6dfe60

- Each element is 8 bytes (double)

- Fourth element is at index [1][1] (row 1, column 1)

- Address = 0x6dfe60 + 3*8 = 0x6dfe60 + 0x18 = 0x6dfe78

10

15

267

33

51

64

100

Error:** Missing closing brace for the inner for loop and the if statement
block. Should be:

If(a[i][j]>100)

Break;

Int count = 0;

For(int i=0; i<3 && count<3; i++) {


For(int j=0; j<2 && count<3; j++) {

Cout << a[i][j] << endl;

Count++;

Int elementCount = 0;

For(int i=0; i<3; i++) {

For(int j=0; j<2; j++) {

elementCount++;

if(elementCount == 3) continue;

cout << a[i][j] << endl;

11

[Address stored in p1]

[Address stored in p2]

[Address stored in p1] + 8

48

[Address of newly allocated memory]

40

Explanation:**

1. `int a=40; int **p1, **p2;` - declares variables

2. `p1=&a;` - p1 now points to a (incorrect, should be int*)


3. `p2=&p1;` - p2 points to p1

4. First cout prints address of a

5. Second cout prints address of p1

6. `**p2=*p1+8;` - dereferences p2 twice to modify a (a becomes


40+8=48)

7. Next cout shows p1’s value (address of a)

8. Next cout shows a’s new value (48)

9. `p1=new int;` - p1 now points to new memory

10. `*p1=45;` - assigns 45 to that memory

11. Next cout shows p1’s new value

12. Next cout shows a is still 48

13. `p1=num;` - p1 points to num array

14. `*(p1+2)=*p1+3;` - num[2] = num[0]+3 = 5+3 = 8

15. Loop prints modified array: 5,4,8,9,8

**Errors:**

- p1 and p2 declared as int** but used incorrectly

- Missing quotes in cout statements

`**p2=”p1+8;` has a typo (extra quote)

You might also like