Question

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. SP19-BCS-154 (Huma Khalid)

NOTE: 10

1)USE C LAUNGAGE ONLY AND NO FILE HANDLING ALLOWED

2)You can use string tokenization for seprating delimeters then save roll and names in two different arrays named as student_name and student_rollno i.e Parallel arrays and then display both the arrays.

3)Again use the string tokenization to separate Programs in student_rollno(using your roll no array) with the help of strcmp() function to separate BCS students from BSE in loop and in the same loop make sure that only BCS students of roll no between 100-200 are chosen and make those roll no and student names printed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* cpy_str(char *source)
{
char *target=(char*)malloc(sizeof(source));
int i=0;
while(source[i]!='\0')
{
target[i]=source[i];
i++;
}
target[i]='\0';
return target;
}
int main()
{
char str[]="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)";;
char *roll_no[100];
char *names[100];
int count=0;
char* token;
char *str_ptr=str;
char* name_ptr;
char* rno_ptr;
while ((token = strtok_r(str_ptr,",",&str_ptr)))
{
  
rno_ptr= strtok_r(token,"(",&token);
roll_no[count]=rno_ptr;
  
name_ptr= strtok_r(token,")",&token);
names[count]=name_ptr;
  
count=count+1;

}
printf("List:\n");
  
for(int i=0;i<count;i++)
{
printf("%s(%s)\n", roll_no[i],names[i]);
}
printf("BCS students of roll no between 100-200\n");
for(int i=0;i<count;i++)
{
char* token1="",*token2="",*token3="";
rno_ptr=cpy_str(roll_no[i]);
  
//printf("%s\n", rno_ptr);
token1=strtok(rno_ptr,"-");
token2=strtok(NULL,"-");
token3=strtok(NULL,"\0");
//printf("%s\n", roll_no[i]);
//printf("%s\n", token1);
//printf("%s\n", token2);
//printf("%s\n", token3);
  
if((strcmp(token2,"BCS"))==0)
{
int temp=atoi(token3);
if((temp>=100)&&(temp<=200))
{
printf("%s(%s)\n", roll_no[i],names[i]);
}
}
  

}
  
return 0;
}
OUTPUT:

List:
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)
BCS students of roll no between 100-200
SP19-BCS-154(Huma Khalid)

Program with no strtok_r and without malloc function


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
char str[]="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)";;
char *list[100];
char *roll_no[100];
char *names[100];
int count=0;
char* list_ptr;
char *str_ptr=str;

char *tk = strtok(str_ptr,",");
while (tk!=NULL)
{   
list[count]=tk;
//printf("%s\n",list[count]);
tk= strtok(NULL,",");  
count=count+1;
}
for(int i=0;i<count;i++)
{
tk = strtok(list[i],"(");
if(tk!=NULL)
{
   roll_no[i]=tk;
}
tk= strtok(NULL,")");
if(tk!=NULL)
{
   names[i]=tk;
}
}
printf("List:\n");
  
for(int i=0;i<count;i++)
{
//printf("%s\n",list[i]);
printf("%s(%s)\n", roll_no[i],names[i]);
}

printf("BCS students of roll no between 100-200\n");
for(int i=0;i<count;i++)
{
char* token1="",*token2="",*token3="";
char rno_ptr[100];
for(int j=0;roll_no[i][j]!='\0';j++)
{
   rno_ptr[j]=roll_no[i][j];
}
  

token1=strtok(rno_ptr,"-");
token2=strtok(NULL,"-");
token3=strtok(NULL,"\0");

  
if((strcmp(token2,"BCS"))==0)
{
int temp=atoi(token3);
if((temp>=100)&&(temp<=200))
{
printf("%s(%s)\n", roll_no[i],names[i]);
}
}
  

}
  
return 0;
}
OUTPUT:

List:
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)
BCS students of roll no between 100-200
SP19-BCS-154(Huma Khalid)

Add a comment
Know the answer?
Add Answer to:
Write a program, which reads a list of student records from a file in batch mode....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • Write a program that processes a data file of names in which each name is on...

    Write a program that processes a data file of names in which each name is on a separate line of at most 80 characters. Here are two sample names: Hartman-Montgomery, Jane R. Doe, J. D. Strings On each line the surname is followed by a comma and a space. Next comes the first name or initial, then a space and the middle initial. Your program should scan the names into three arrays— surname , first , and middle_init . If...

  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

  • In C++, write a complete program that receives a series of student records from the keyboard...

    In C++, write a complete program that receives a series of student records from the keyboard and stores them in three parallel arrays named studentID and courseNumber and grade. All arrays are to be 100 elements. The studentID array is to be of type int and the courseNumber and grade arrays are to be of type string. The program should prompt for the number of records to be entered and then receive user input on how many records are to...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • IN C++ Write a program in c++ that will read from a file the following sentence:...

    IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • Write a C++ program to store and update students' academic records in a binary search tree....

    Write a C++ program to store and update students' academic records in a binary search tree. Each record (node) in the binary search tree should contain the following information fields:               1) Student name - the key field (string);               2) Credits attempted (integer);               3) Credits earned (integer);               4) Grade point average - GPA (real).                             All student information is to be read from a text file. Each record in the file represents the grade information on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT