Pointer

Fundamental About Pointer
    #include <stdio.h>
    #include <stdlib.h>
    int main() {
    int *ptr, n, i;
    printf("Enter number of elements: ");
    scanf("%d", &n);
    ptr = malloc(n * sizeof(int)); // No need for (int *) as it's implicit
    if (!ptr) return 1; // Checks if malloc fails

    printf("Enter the values:\n");
    for (i = 0; i < n; i++) {
    scanf("%d", &ptr[i]);
    }

    printf("Values entered are:\n");
    for (i = 0; i < n; i++) {
    printf("%d ", ptr[i]);
    }

    free(ptr); // Free the dynamically allocated memory
    return 0;
    }



  1. Write a program to accept two number and sort them with using pointer.
    #include <stdio.h>

    // Function to sort two numbers using pointers
    void sortNumbers(int *ptr1, int *ptr2) {
        if (*ptr1 > *ptr2) {
            // Swap values using pointers
            int temp = *ptr1;
            *ptr1 = *ptr2;
            *ptr2 = temp;
        }
    }

    int main() {
        int num1, num2;
       
        // Input two numbers from user
        printf("Enter first number: ");
        scanf("%d", &num1);
        printf("Enter second number: ");
        scanf("%d", &num2);

        // Call function to sort numbers
        sortNumbers(&num1, &num2);

        // Print sorted numbers
        printf("Sorted numbers: %d %d\n", num1, num2);

        return 0;
    }




  2. Write a function that receive marks received by a student in subjects and return the average and percentage of these marks call this function form main() and print the results in main().
    #include <stdio.h>

    // Function to calculate average and percentage of marks
    void calculateMarks(int marks[], int n, double *average, double *percentage) {
        int total_marks = 0;
       
        // Calculate total marks
        for (int i = 0; i < n; ++i) {
            total_marks += marks[i];
        }
       
        // Calculate average
        *average = (double)total_marks / n;
       
        // Calculate percentage
        *percentage = ((double)total_marks / (n * 100)) * 100;
    }

    int main() {
        int n;
        printf("Enter number of subjects: ");
        scanf("%d", &n);
       
        int marks[n];
       
        // Input marks from user
        printf("Enter marks for %d subjects:\n", n);
        for (int i = 0; i < n; ++i) {
            printf("Enter marks for subject %d: ", i + 1);
            scanf("%d", &marks[i]);
        }
       
        double average, percentage;
       
        // Call function to calculate average and percentage
        calculateMarks(marks, n, &average, &percentage);
       
        // Print results
        printf("\nResults:\n");
        printf("Average marks: %.2f\n", average);
        printf("Percentage: %.2f%%\n", percentage);
       
        return 0;
    }





  3. Write a function that receives 5 integers and return the sum, average and stand deviation of these numbers. Call this function form main() and print the result in main().
    #include <stdio.h>
    #include <math.h> // Include math.h for sqrt() function

    // Function to calculate sum, average, and standard deviation
    void calculateStats(int arr[], int n, int *sum, double *avg, double *std_dev) {
        // Calculate sum
        *sum = 0;
        for (int i = 0; i < n; ++i) {
            *sum += arr[i];
        }

        // Calculate average
        *avg = (double)(*sum) / n;

        // Calculate standard deviation
        double sum_of_squares = 0.0;
        for (int i = 0; i < n; ++i) {
            sum_of_squares += (arr[i] - *avg) * (arr[i] - *avg);
        }
        *std_dev = sqrt(sum_of_squares / n);
    }

    int main() {
        int numbers[5];
        int sum;
        double average, standard_deviation;

        // Input 5 integers from user
        printf("Enter 5 integers:\n");
        for (int i = 0; i < 5; ++i) {
            printf("Enter integer %d: ", i + 1);
            scanf("%d", &numbers[i]);
        }

        // Call function to calculate sum, average, and standard deviation
        calculateStats(numbers, 5, &sum, &average, &standard_deviation);

        // Print results
        printf("\nResults:\n");
        printf("Sum: %d\n", sum);
        printf("Average: %.2f\n", average);
        printf("Standard Deviation: %.2f\n", standard_deviation);

        return 0;
    }






  4. Write a C function to evaluate the series.
    sin(x)=x-(x^3/3!)+(x^5/5!)-(x^7/7!)+..
    up to 10 terms.
    #include <stdio.h>
    #include <math.h> // Include math.h for pow() function

    // Function to calculate factorial
    unsigned long long factorial(int n) {
        if (n == 0 || n == 1) {
            return 1;
        } else {
            unsigned long long fact = 1;
            for (int i = 2; i <= n; ++i) {
                fact *= i;
            }
            return fact;
        }
    }

    // Function to evaluate sine function using Taylor series up to 10 terms
    double sineSeries(double x) {
        double result = 0.0;
        int sign = 1;

        // Calculate series up to 10 terms
        for (int i = 0; i < 10; ++i) {
            int exponent = 2 * i + 1;
            double term = sign * pow(x, exponent) / factorial(exponent);
            result += term;
            sign = -sign; // Alternate sign for next term
        }

        return result;
    }

    int main() {
        double x;

        // Input value of x (in radians)
        printf("Enter the value of x (in radians): ");
        scanf("%lf", &x);

        // Evaluate sine using the function
        double result = sineSeries(x);

        // Output the result
        printf("sin(%.2f) = %.6f\n", x, result);

        return 0;
    }




  5. Given Three Variables x,y,z write a function to circularly shift their value to right. In other words if x=5, y=8, z=10, after circular shift y=5, z=8, x=10. Call the function with variable a,b,c to circularly shift value.
    #include <stdio.h>

    // Function to circularly shift values to the right
    void circularShiftRight(int *x, int *y, int *z) {
        int temp = *z;
        *z = *y;
        *y = *x;
        *x = temp;
    }

    int main() {
        int a = 5, b = 8, c = 10;

        // Print initial values
        printf("Before circular shift:\n");
        printf("a = %d, b = %d, c = %d\n", a, b, c);

        // Perform circular shift
        circularShiftRight(&a, &b, &c);

        // Print shifted values
        printf("After circular shift:\n");
        printf("a = %d, b = %d, c = %d\n", a, b, c);

        return 0;
    }




  6. Write a program to find the sum of all the elements of an array using pointers.
    #include <stdio.h>

    int main() {
        int arr[] = {1, 2, 3, 4, 5};
        int *ptr = arr; // Pointer to the first element of the array
        int sum = 0;

        // Iterate through the array using the pointer and calculate the sum
        while (*ptr) {
            sum += *ptr;
            ptr++; // Move the pointer to the next element
        }

        // Print the sum
        printf("Sum of array elements: %d\n", sum);

        return 0;
    }










0 Comments had been done yet:

Post a Comment