Skip to main content

Pass student array; display passed students

Short answer

Parallel arrays (name[][20], roll[], addr[][20], marks[]) are passed by name. The function calls isPass(marks) to check marks>=40, then displays only passed students, using array arguments for direct access.

Can we pass whole array element from function? Write a program to display only those students information which are passed. Use separate function to check the result of student. The information of students like Name, Roll No, Address and Marks are passed from main functions and pass to functions using array type arguments.

Yes. The whole array is passed to a function by writing only its array name in the call. The function gets the address of the first element and can read or change every element. The size must be passed as another argument.

Program

The program keeps the student information in four parallel arrays (name, roll, address, marks) and passes them to showPassed(). That function calls a separate function isPass() to check the result of each student (passing marks = 40). Only the passed students are displayed.

#include <stdio.h>
#define N 5

/* returns 1 if the student has passed (marks >= 40) */
int isPass(int marks)
{
    return marks >= 40;
}

void showPassed(char name[][20], int roll[], char addr[][20], int marks[], int n)
{
    printf("\nPassed students\n");
    printf("%-10s %-6s %-12s %s\n", "Name", "Roll", "Address", "Marks");
    for (int i = 0; i < n; i++)
        if (isPass(marks[i]))
            printf("%-10s %-6d %-12s %d\n", name[i], roll[i], addr[i], marks[i]);
}

int main(void)
{
    char name[N][20], addr[N][20];
    int roll[N], marks[N];
    for (int i = 0; i < N; i++) {
        printf("Student %d (name roll address marks): ", i + 1);
        scanf("%s %d %s %d", name[i], &roll[i], addr[i], &marks[i]);
    }
    showPassed(name, roll, addr, marks, N);
    return 0;
}

Sample run

Input: Ram 1 Kathmandu 65 Sita 2 Lalitpur 32 Hari 3 Bhaktapur 48 Gita 4 Pokhara 39 Bibek 5 Butwal 71

Student 1 (name roll address marks): Student 2 (name roll address marks): Student 3 (name roll address marks): Student 4 (name roll address marks): Student 5 (name roll address marks): 
Passed students
Name       Roll   Address      Marks
Ram        1      Kathmandu    65
Hari       3      Bhaktapur    48
Bibek      5      Butwal       71

Sita (32) and Gita (39) have failed, so they are not displayed.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 7 questions →