حل lab5 برمجة1 جامعة الامام

  • 2025-10-12

توصيف

Lab 5 (1 page, 6 Exercises)

Write a single C++ program where you call all the following functions.

 

Exercise 1

Write a C++ function readArray that reads an array of n integers.

#include <iostream>
using namespace std;

// Function to read an array of n integers
void readArray(int arr[], int n) {
    cout << "Enter " << n << " integers:" << endl;
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin >> n;

    int arr[100]; // assuming max size is 100 for simplicity

    if (n > 0 && n <= 100) {
        readArray(arr, n);

        // Optional: display the array
        cout << "You entered: ";
        for (int i = 0; i < n; ++i) {
            cout << arr[i] << " ";
        }
        cout << endl;
    } else {
        cout << "Invalid size. Please enter a number between 1 and 100." << endl;
    }

    return 0;
}

 

Exercise 2

Write a C++ function printArray that prints an array of n integers.

#include <iostream>
using namespace std;

// Function to print an array of n integers
void printArray(int arr[], int n) {
    cout << "Array elements: ";
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    int arr[] = {5, 10, 15, 20, 25};
    int n = sizeof(arr) / sizeof(arr[0]);

    printArray(arr, n);

    return 0;
}

 

Exercise 3

Write a C++ function minimum that returns the minimum of an array of n integers.

#include <iostream>
using namespace std;

// Function to find the minimum value in an array of n integers
int minimum(int arr[], int n) {
    int minVal = arr[0]; // Assume first element is the minimum

    for (int i = 1; i < n; ++i) {
        if (arr[i] < minVal) {
            minVal = arr[i];
        }
    }

    return minVal;
}

int main() {
    int arr[] = {8, 3, 15, -4, 10};
    int n = sizeof(arr) / sizeof(arr[0]);

    int minResult = minimum(arr, n);
    cout << "Minimum value in the array is: " << minResult << endl;

    return 0;
}

 

Exercise 4

Write a C++ function minindex that returns the index of the minimum of an array of n integers.

#include <iostream>
using namespace std;

// Function to find the index of the minimum value in an array
int minIndex(int arr[], int n) {
    int minIdx = 0;

    for (int i = 1; i < n; ++i) {
        if (arr[i] < arr[minIdx]) {
            minIdx = i;
        }
    }

    return minIdx;
}

int main() {
    int arr[] = {12, 5, 7, -3, 9};
    int n = sizeof(arr) / sizeof(arr[0]);

    int index = minIndex(arr, n);
    cout << "The index of the minimum value is: " << index << endl;
    cout << "Minimum value: " << arr[index] << endl;

    return 0;
}

 

Exercise 5

Write a C++ function crshift that performs a circular right shift on an array of n integers.

#include <iostream>
using namespace std;

// Function to perform a circular right shift on an array of n integers
void crshift(int arr[], int n) {
    if (n <= 1) return; // No need to shift if array has 0 or 1 element

    int last = arr[n - 1]; // Save the last element

    // Shift elements to the right
    for (int i = n - 1; i > 0; --i) {
        arr[i] = arr[i - 1];
    }

    arr[0] = last; // Place last element at the beginning
}

// Helper function to print the array
void printArray(int arr[], int n) {
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    cout << "Original array: ";
    printArray(arr, n);

    crshift(arr, n);

    cout << "After circular right shift: ";
    printArray(arr, n);

    return 0;
}

 

Exercise 6

Write a C++ function sort that sorts an array of n integers.

#include <iostream>
using namespace std;

// Function to sort an array of n integers (using selection sort)
void sortArray(int arr[], int n) {
    for (int i = 0; i < n - 1; ++i) {
        int minIdx = i;

        // Find the index of the smallest element in the rest of the array
        for (int j = i + 1; j < n; ++j) {
            if (arr[j] < arr[minIdx]) {
                minIdx = j;
            }
        }

        // Swap the current element with the smallest found
        if (minIdx != i) {
            int temp = arr[i];
            arr[i] = arr[minIdx];
            arr[minIdx] = temp;
        }
    }
}

// Helper function to print the array
void printArray(int arr[], int n) {
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}

int main() {
    int arr[] = {8, 3, 5, 1, 9, 2};
    int n = sizeof(arr) / sizeof(arr[0]);

    cout << "Original array: ";
    printArray(arr, n);

    sortArray(arr, n);

    cout << "Sorted array: ";
    printArray(arr, n);

    return 0;
}

هل كان المحتوى مفيد؟

التعليقات

لاضافة سؤال أو تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول
محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...