Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
jobrod
/
HW1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d608c140
authored
4 days ago
by
jobrod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
master
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
90 additions
and
0 deletions
HW1code.c
HW1code.c
0 → 100644
View file @
d608c140
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_RESIDENTS 100
#define MAX_LENGTH 100
// Define the structures
typedef
struct
{
char
student_code
[
MAX_LENGTH
];
char
name
[
MAX_LENGTH
];
char
id_code
[
MAX_LENGTH
];
}
Student
;
typedef
struct
{
char
id_code
[
MAX_LENGTH
];
char
city
[
MAX_LENGTH
];
}
Resident
;
// Function to read students from F1.txt
int
read_students
(
const
char
*
file_name
,
Student
students
[])
{
FILE
*
file
=
fopen
(
file_name
,
"r"
);
if
(
file
==
NULL
)
{
perror
(
"Error opening file"
);
return
-
1
;
}
int
count
=
0
;
while
(
fscanf
(
file
,
"%[^,],%[^,],%s
\n
"
,
students
[
count
].
student_code
,
students
[
count
].
name
,
students
[
count
].
id_code
)
!=
EOF
)
{
count
++
;
}
fclose
(
file
);
return
count
;
}
// Function to read residents from F2.txt
int
read_residents
(
const
char
*
file_name
,
Resident
residents
[])
{
FILE
*
file
=
fopen
(
file_name
,
"r"
);
if
(
file
==
NULL
)
{
perror
(
"Error opening file"
);
return
-
1
;
}
int
count
=
0
;
while
(
fscanf
(
file
,
"%[^,],%s
\n
"
,
residents
[
count
].
id_code
,
residents
[
count
].
city
)
!=
EOF
)
{
count
++
;
}
fclose
(
file
);
return
count
;
}
// Main function
int
main
()
{
Student
students
[
MAX_STUDENTS
];
Resident
residents
[
MAX_RESIDENTS
];
int
student_count
=
read_students
(
"F1.txt"
,
students
);
int
resident_count
=
read_residents
(
"F2.txt"
,
residents
);
if
(
student_count
==
-
1
||
resident_count
==
-
1
)
{
return
1
;
}
char
residence
[
MAX_LENGTH
];
printf
(
"Enter the residence: "
);
scanf
(
"%s"
,
residence
);
// Find matching students
int
found
=
0
;
for
(
int
i
=
0
;
i
<
student_count
;
i
++
)
{
for
(
int
j
=
0
;
j
<
resident_count
;
j
++
)
{
if
(
strcmp
(
students
[
i
].
id_code
,
residents
[
j
].
id_code
)
==
0
&&
strcmp
(
residents
[
j
].
city
,
residence
)
==
0
)
{
if
(
!
found
)
{
printf
(
"Students with residence %s:
\n
"
,
residence
);
found
=
1
;
}
printf
(
"Student Code: %s, Name: %s, ID Code: %s
\n
"
,
students
[
i
].
student_code
,
students
[
i
].
name
,
students
[
i
].
id_code
);
}
}
}
if
(
!
found
)
{
printf
(
"No students found with the given residence.
\n
"
);
}
return
0
;
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment