Question

In: Computer Science

Write a program, which reads a list of student records from a file in batch mode....

Write a program, which reads a list of student records from a file in batch mode. Each student record comprises a roll number and student name, and the student records are separated by commas. An example data in the file is given below.

· SP18-BCS-050 (Ali Hussan Butt), SP19-BCS-154 (Huma Khalid), FA19-BSE-111 (Muhammad Asim Ali), SP20-BSE-090 (Muhammad Wajid), SP17-BCS-014 (Adil Jameel)

The program should store students roll numbers and names separately in 2 parallel arrays named names and rolls, i.e. in the above case, 1st element of names contains “Ali Hussan Butt” and 1st element of rolls contain “SP18-BCS-321”; similarly, 2nd element of names contains “Huma Khalid” and 2nd element of rolls contain “SP19-BCS-154” and so on. Hint: You may use delimiters string “() ,”. Ignore empty strings returned as tokens (i.e. strings with length 0), if any.

In the end, first, display roll numbers and names for only the following students:

· Who are enrolled in BCS program.

· Whose roll number is between 100 and 200. (Hint: you can extract last 3 characters of roll number, convert it into integer and then check range 100-200)

For example, in the above case, only following students should be displayed.

1. Ali Hussan Butt, roll number: SP18-BCS-050

2. Adil Jameel, roll number: SP17-BCS-014

NOTE:USE ONLY C LAUNGAGE AND DONT USE FILE HANDLING.

Solutions

Expert Solution

import os
from itertools import islice
cwd = "/home/dexter/Desktop"    # working directory
path = cwd + "/student.txt"         # name of the text file
f1 = open(path, "r")            # open text file in read mode
numLines = sum(1 for line in f1)    # counting the numbr of lines in the text file
f1.close()                      # closing the text file
begin = 0   # start reading the text file from index [0]
end = numLines #read the text file till the last row

tempArray=[]    # temporary array
rolls=[]    # array to store roll no.
names=[]    # array to store name

with open(path, 'r') as infile:
    lines_gen = islice(infile, begin, end) # reading the text file from row[0] till last row
                                            #here it is only 1 row becaue the data is comma (') separated only
    for line in lines_gen:
        tempArray=line.split(",")   # array to store comma (') separated values

for i in range(len(tempArray)):
    temp=tempArray[i].split("(")[0]     #roll no and names are separated by "("
                                        # first part is the roll no. having a whitespace as the last character
    temp=temp.split(" ")[0]             # removing the last character i.e. the whitespace
    rolls.append(temp)              # storing the roll no. in array

    temp=tempArray[i].split("(")[1]     #roll no and names are separated by "("
                                        # second part is the name having a ")" as the last character
    temp=temp.split(")")[0]             # removing the last character i.e. ")"
    names.append(temp)              # storing the name in array

for i in range(len(rolls)):
    currentRoll=rolls[i]    #stores the current roll no.
    if (currentRoll.find("BCS") != -1): # if line contains substring "BCS"
        rollNum=int(currentRoll.split("-")[2])   #the 3rd part contains the roll number, converting into integer
        if not(rollNum>=100    and      rollNum<=200): #if the roll num lies between 100 & 200
            print(names[i],",roll number: ",rolls[i])



Related Solutions

Write a program that processes numbers, corresponding to student records read in from a file, and...
Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions: double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers. int read_integer (FILE *infile) - Reads one integer number from the input file. double calculate_sum (double number1, double number2, double...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts them into a string array, and sorts the array in alphabetical order. String objects can be compared using relational operators such as <, >, or ==. For example, “abc” > “abd” is false, but “abc” < “abd” is true. Sample output: Before Sorting: Cherry, Honeydew, Cranberry, Lemon, Orange, Persimmon, Watermelon, Kiwifruit, Lime, Pomegranate, Jujube, Pineapple, Durian, Plum, Banana, Coconut, Apple, Tomato, Raisin, Mandarine, Blackberry,...
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT