Programlama dersini alan tüm öğrencilere ait bilgiler aşağıda verilen struct ile rastgele erişimli bir dosyada tutulmaktadır. Dosya içerisinde bulunan iki kaydın bilgilerinin yer değiştirilmesi istenmektedir. Bu işlemi gerçekleştiren kodu yazınız. Note: The position of the 2 records that you need to swap like this: The position of the first record is the sum of the first 5 digits of your student number. The position of the second record is the sum of the last 5 digits of your student number. Örnek (Examples): if your student number is : 1910202034 : then you need to swap recods 13 <--> 9 if your student number is: 2014010217021 : then you need to swap recods 7 <--> 11 Dosya (File): struct student { 1.record int number; 2.record float midterm: 3.record char name[40]: n.record

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
I need answer with C language Fast please
Programlama dersini alan tüm öğrencilere ait bilgiler aşağıda verilen struct ile rastgele erişimli bir dosyada tutulmaktadır. Dosya içerisinde bulunan iki kaydın bilgilerinin yer değiştirilmesi istenmektedir. Bu işlemi
gerçekleştiren kodu yazınız.
Note: The position of the 2 records that you need to swap like this:
The position of the first record is the sum of the first 5 digits of your student number.
The position of the second record is the sum of the last 5 digits of your student number.
Örnek (Examples):
if your student number is : 1910202034
: then you need to swap recods 13 <--> 9
if your student number is : 2014010217021 : then you need to swap recods
7 <--> 11
Dosya (File):
struct student {
1.record
int number;
2.record
float midterm:
3.record
char name[40]:
n.record
Transcribed Image Text:Programlama dersini alan tüm öğrencilere ait bilgiler aşağıda verilen struct ile rastgele erişimli bir dosyada tutulmaktadır. Dosya içerisinde bulunan iki kaydın bilgilerinin yer değiştirilmesi istenmektedir. Bu işlemi gerçekleştiren kodu yazınız. Note: The position of the 2 records that you need to swap like this: The position of the first record is the sum of the first 5 digits of your student number. The position of the second record is the sum of the last 5 digits of your student number. Örnek (Examples): if your student number is : 1910202034 : then you need to swap recods 13 <--> 9 if your student number is : 2014010217021 : then you need to swap recods 7 <--> 11 Dosya (File): struct student { 1.record int number; 2.record float midterm: 3.record char name[40]: n.record
Expert Solution
Step 1

/Main.c//////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//_______
struct student{
    int number;
    float midterm;
    char name[40];
};
//_______

//_______
int swapRecord(FILE *filePtr, int first, int last)
{
        fseek(filePtr, 0, SEEK_SET);
    int count=1;
    struct student *stud,*stud1=NULL,*stud2=NULL;
        while (!feof(filePtr)){
        stud=(struct student*)malloc(sizeof(struct student));
                int result = fread(stud, sizeof(struct student), 1, filePtr);
                if (result != 0 && count==first){
            stud1=stud;
                }
        else if(result != 0 && count==last){
            stud2=stud;
        }
        count++;
        }
    if(stud1==NULL || stud2==NULL)
        return 0;
    fseek(filePtr, 0, SEEK_SET);
    count=1;
    
    while (!feof(filePtr)){
        fread(stud, sizeof(struct student), 1, filePtr);
        if(count==first){
            fseek(filePtr, -(long)sizeof(struct student), 1);
            fwrite(stud2, sizeof(struct student), 1, filePtr);
            if(first>last)
                return 1;
        }
        if(count==last){
            fseek(filePtr, -(long)sizeof(struct student), 1);
            fwrite(stud1, sizeof(struct student), 1, filePtr);
            if(last>first)
                return 1;
        }
        count++;
        //fseek(filePtr, (long)sizeof(struct student), 1);
    }
    return 1;
}
//_________
void addStudent(FILE *filePtr, int number, float midterm, char name[40]){
        struct student student;
        student.number=number;
    strcpy(student.name,name);
    student.midterm=midterm;
    fwrite(&student, sizeof(struct student), 1, filePtr);
}
//_______
void showRecords(FILE *filePtr){
    fseek(filePtr, 0, SEEK_SET);
    printf("\n%-6s %-8s %-20s\n","Number","Midterm","Name");
    while (!feof(filePtr)){
        struct student student;
        int result = fread(&student, sizeof(struct student), 1, filePtr);
        if (result != 0){
            printf("%-6d %-8f %-20s\n",student.number,student.midterm,student.name);
        }
    }
}
//_______
int main(){
    char roll[30];
    printf("Enter roll number: ");
    scanf("%s",roll);
    int len=strlen(roll);
    int last=0,first=0;
    
    for(int i=0;i<5;i++){
        first=first+roll[i]-48;
        last=last+roll[len-1-i]-48;
    }

    printf("First record number: %d\n",first);
    printf("Last record number: %d\n",last);

    FILE *filePtr;
    filePtr = fopen("input.txt","wb+");
    if (filePtr == NULL){
        printf("Could not open input.txt");
        return 0;
    }
    addStudent(filePtr,54,67.8,"Alex");
    addStudent(filePtr,67,90,"Bob");
    addStudent(filePtr,23,69,"Dillon");
    addStudent(filePtr,89,80,"Mojesh");

    showRecords(filePtr);
    if(swapRecord(filePtr,first,last)==0){
        printf("Invalid record number!\n");
        return 0;
    }
    printf("\nRecord number %d is swapped with record number %d\n",first,last);
    showRecords(filePtr);
    fclose(filePtr);
    return 0;
}
//end of Main.c//////////////////////

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Structured English
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education