hash table program that will take the first character of a string - c

I need to make a hash table program that will take the first character of a string in a small case letter and modulo the ASCII with the size of the hash table that the user input. Where should I input the size other than in the main?
These are my current codes. In this code, I ask the user to input the size in the main module function but I am confused about how to use it in on every function besides main. Any help is appreciated.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
struct data
{
char name [100];
char age[2];
struct data *next;
};
struct data *chain[size] = {NULL};
struct data* insert(char name[], char age[]){
struct data *curr = (struct data*) malloc (sizeof(data));
strcpy(curr->name, name);
printf("input name: ");
scanf(" %[^\n]s",strcpy(curr->name, name));
printf("input age: ");
scanf(" %[^\n]s", strcpy(curr->age, age));
curr->next = NULL;
return curr;
};
int main(){
int n;
char name[100];
char age[2];
char firstChar;
int key;
int index = 0;
printf("input the number of hash table: ");
scanf("%d", &n); getchar();
printf("\n");
int option;
do{
printf("=== Option Menu ===\n");
printf("1. insert data\n");
printf("2. delete data\n");
printf("3. search data\n");
printf("4. view data\n");
printf("5. exit\n");
printf("input option: ");
scanf("%d", &option); getchar();
switch(option){
case 1:
insert(name,age);
break;
}
}while(option!=5);
}

can i make the size of hashtable dynamic ?
Yes, you can, and you even need to, since you wrote "I need to make a hash table program … with the size of the hash table that the user input". In order to do that, don't define struct data *chain[size] = {NULL}; but struct data **chain; and after input of n in main do
chain = calloc(n, sizeof *chain);

Related

How do I pass a airplane row and seat to a txt file together with its passenger name etc.?

We need to make a program for airplane seat reservation. Like we need a program which can reserve, change, delete, and display seats. But our program needs also to be written in a txt file as the user inputs data (name, age, city, seat-e.g., 1F). And I have made a program myself. It runs but exits halfway. And I am not sure if what I'm doing is right. I kind of get the gist on how I can pass informations using structs. But I don't know how I will the seats also in the txt file.
I have these on my first lines:
#include<stdio.h>
#include<malloc.h>
struct Passenger{
int pnum;
int age;
char city[50];
char name[50];
};
struct Seats{
int row;
char seats[10];
};
struct Airplane{
struct Seats seats[6];
};
void intializeDefault(struct Airplane *airplane){
for(int i=0;i<10;i++){
char ch='A';
airplane->seats[i].row=i+1;
for(int j=0;j<6;j++){
airplane->seats[i].seats[j]=ch;
ch++;
}
}
}
And well we kind of have commands, so I put it like:
int main(){
int ch;
struct Airplane airplane;
intializeDefault(&airplane);
do{
printf("Welcome to our Airlines!\n");
printf("\nOptions:");
printf("\n[1] Book a seat.");
printf("\n[2] Display seats.");
printf("\n[3] Change seat.");
printf("\n[4] Delete reservation.");
printf("\n[5] Display reservation records.");
printf("\n[0] Exit.");
printf("\nWhat can we do for you? (Enter number.): ");
scanf("%d", &ch);
switch(ch){
case 1:book();
break;
case 2:display(&airplane);
break;
case 3:change();
break;
case 4:deleteSeat();
break;
case 5:record();
break;
default:
break;
}
}while (ch!=0);
{
printf("\nThank you for booking !");
};
return 0;
}
What I need help on is how should I do on this part:
void book(){
struct Passenger *p;
FILE *fp;
struct Airplane airplane;
intializeDefault(&airplane);
int totalSeats = 60;
int seatsBooked = 0;
int n, i, j, row;
char seat;
printf("How many passengers/seats you want to reserve?: ");
scanf("%d", &n);
p = (struct Passenger*) malloc(n * sizeof(struct Passenger));
fp=fopen("Passengers.txt", "w");
if(seatsBooked==60){
printf("\nAll seats are booked now.");
}
for(i=0;i<n;i++){
printf("Enter Roll Number: ");
scanf("%d", p[i].pnum);
fflush(stdin);
printf("Enter #%d passenger's name: ", i+1);
scanf("%[^\n]s", p[i].name);
fflush(stdin);
printf("What's the passenger's age?: ");
scanf("%d", p[i].age);
fflush(stdin);
printf("Please enter the city where the passenger resides: ");
scanf("%d",p[i].city);
fflush(stdin);
printf("\nEnter Seat row: (Type 1-10) ");
scanf("%d", &row);
fflush(stdin);
printf("Enter Seat column: (Type A-F) ");
scanf("%c", &seat);
if(airplane.seats[row-1].seats[seat-'A']!='X'){
airplane.seats[row-1].seats[seat-'A']='X';
seatsBooked++;
printf("\nSeat %d%c is booked. \n\n", row, seat);
}
else{
printf("\nSeat %d%c is already booked.\n", row, seat);
}
}
fclose(fp);
}
I just need to know on how I can also pass every passenger's seats within on them, and I think I will figure out the other parts. It's because we need to show the records of passengers and their seat reservations when we command "5", and I need to show "X" on every reserved seats when we command "2". And I am not quite sure how I will do that. Should I change the functions on above? The structs? Or some lines?
I gave a quick look and I think that you should keep track of the booked seat by the single user... In your "Seats" struct, why don't you add an array of "Passenger" pointers in order to link every seat to the respective user?
As soon a user books a seat, you should have the index of the booked seat in a specific row. You take the char array and you put an 'X' in that position. Then you take the Passenger* array and in the same position you assign the address of that client's Passenger object.
The one suggestion I would make is to pass along your airplane structure to your various functions. For example:
switch(ch){
case 1:book(&airplane);
break;
Then, down in your "book" function you would use the airplane structure in your code.
void book(Airplane *airplane){
struct Passenger *p;
FILE *fp;
//struct Airplane airplane; /* Or just remove this line of code */
You might want to repeat the referencing of the airplane structure in your other functions as well.
Hope that helps.
Regards.

How to make the series of questions asked?

Here is my code. I want it to ask me questions in a sequence. But whenever I enter my choice and put my name it didn't allow me to ask further. How to deal with that?
#include <stdio.h>
#include <stdlib.h>
int new_acc();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, new_account;
printf("-----WELCOME TO THE MAIN MENU-----\n\n");
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
if (scanf("%d",&one)){
new_account = new_acc(); // calling a function
}
return 0;
}
int new_acc(){
int id; char name;
printf("Enter your name: ");
scanf("%c\n",&name);
printf("Enter your ID card number: ");
scanf("%d\n",&id);
return 0;
}
If you typed Enter after typing nubmer for the MAIN MENU, the newline character remains in the buffer.
Then, is is read as the name via %c.
After that, if you typed, for example, alphabet as name, it will prevent it from reading the number id.
To avoid this, you can put a space before %c to have it skip the newline character.
Also you won't be have to skip after reading name and id, so you should remove \n in scanf() after %c and %d for them.
int new_acc(){
int id; char name;
printf("Enter your name: ");
scanf(" %c",&name); /* add space and remove \n */
printf("Enter your ID card number: ");
scanf("%d",&id); /* remove \n */
return 0;
}
By the way, the above code will allow only one alphabet as name.
To support multi-character name (without space character), you should use an array of char and %s with length specified.
int new_acc(){
int id; char name[1024];
printf("Enter your name: ");
scanf(" %1023s",name); /* don't use & here, and size limit is buffer size - 1 (-1 for terminating null character) */
printf("Enter your ID card number: ");
scanf("%d",&id);
return 0;
}
If you want to support name with space characters, you can use %[\n] (read until newline character) instead of %s.
int new_acc(){
int id; char name[1024];
printf("Enter your name: ");
scanf(" %1023[^\n]",name);
printf("Enter your ID card number: ");
scanf("%d",&id);
return 0;
}
Seems like you want to use an object oriented programming paradigm in this. To so do, you should define an "object" with struct and save the new account with that:
#define MAX 50
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Account {
int id;
char name[MAX];
};
struct Account new_acc();
int main(){
int choice;
struct Account new_account;
printf("-----WELCOME TO THE MAIN MENU-----\n\n");
printf("1. Create new account\n");
printf("Enter you choice: ");
scanf("%d",&choice);
switch(choice) {
case 1:
new_account = new_acc();
break;
default:
printf("Not a valid option\n");
return 1;
}
return 0;
}
struct Account new_acc(){
char name[MAX];
int id;
struct Account new;
printf("Enter your name: ");
scanf("%c\n",name);
printf("Enter your ID card number: ");
scanf("%d\n",&id);
strcpy(new.name, name);
new.id = id;
return new;
}
Pay attention because this code is very vulnerable to buffer overflows. Plus, I edited your check for the option in main because scanf returns 1 if reads whatever value successfully.
Use This Code i have modfified a little bit
int new_acc(){
int id; char name[10];
printf("Enter your name: ");
scanf("%s",name);
printf("Enter your ID card number: ");
scanf("%d",&id);
return 0;
}

C - Passing Structure Parameters to Function By Reference and Value

Could I please have an explanation of what I am doing wrong.The program is for inputting and displaying student details. It must use a structure and have 2 functions; one to capture(which is to be passed by reference) and another to display (which is to be passed by value.
Here is my code:
#include <stdio.h>
struct Students{
int ID;
char name[50];
int age;
char address[100];
char course[30];
} aStudent[5];
void capture(char *name , int *age , char *address, char *course){
int i;
for(i=0; i<5; i++){
aStudent[i].ID = i+1;
printf("\nFor Student number %d:\n",aStudent[i].ID);
printf("Enter Student Name: ");
scanf ("%s", &aStudent[i].name);
printf("Enter Student Age: ");
scanf ("%d", &aStudent[i].age);
printf("Enter Student Address: ");
scanf ("%s", &aStudent[i].address);
printf("Enter Course: ");
scanf ("%s", &aStudent[i].course);
}
}
void display(char name, int age , char address, char course){
int i;
for(i=0; i<5; i++){
printf("\nStudent %d:\n",aStudent[i].ID);
printf("Name: %s\t\tAge: %d\t\tAddress: %s\t\tCourse:
%s",aStudent[i].name, aStudent[i].age, aStudent[i].address,
aStudent[i].course);
printf("\n");
}
}
void main()
{
int option, age;
char name, address, course;
printf("\t...Welcome to the Student Data System...\n\n");
printf("\nPlease Select An Option: \n1. Input Student Data\n2. View
Student Data\n3. Exit Syatem\n\n");
scanf("%d",&option);
switch(option){
case 1:
printf("Enter Student Details:\n");
capture(name, age , address, course);
break;
case 2:
printf("\nDisplaying Information:\n");
display(name, age , address, course);
break;
case 3:
close();
break;
default:
printf("\nSorry, your option is not valid.");
}
}
I have tested a number of times and it's working, but I'm getting these error messages:
Errors are shown for every argumety I've used
Also, is there a way or a line(s) of code i can use to return to the start of the switch when I am done with one of the cases - A "Return to Main Menu"?
First of all, variables you have tried to pass (either by value or by reference) when you are calling the two functions capture() and display(), haven't used anywhere because you are dealing directly with the members of the structure Students when you are capturing or displaying the results.
And the reasons you are getting syntax errors are because the capture() is expecting the addresses of the variables (&name,&age,&address,&course) and you are passing variables (name,age,address,course) themselves. And also you are using,
scanf ("%s", &aStudent[i].name);
instead of
scanf ("%s", aStudent[i].name);
In my opinion instead of making the Structure array global, declaring it inside main function and passing the whole structure array to the capture() as a reference and passing it by value to the display() is better to your objective as you need to use both call by value and reference in your code.
I have edited your code a bit and added the return to main menu option. It works for me and I apologize if my answer is long and hard to understand because this is my first answer in stackoverflow. Thanks!
#include <stdio.h>
struct Students
{
int ID;
char name[50];
int age;
char address[100];
char course[30];
};
void capture(struct Students *aStudent)
{
int i;
for(i=0; i<2; i++)
{
aStudent[i].ID = i+1;
printf("\nFor Student number %d:\n",aStudent[i].ID);
printf("Enter Student Name: ");
scanf ("%s", aStudent[i].name);
printf("Enter Student Age: ");
scanf ("%d", &aStudent[i].age);
printf("Enter Student Address: ");
scanf ("%s", aStudent[i].address);
printf("Enter Course: ");
scanf ("%s", aStudent[i].course);
}
}
void display(struct Students aStudent[])
{
int i;
for(i=0; i<2; i++)
{
printf("\nStudent %d:\n",aStudent[i].ID);
printf("Name: %s\t\tAge: %d\t\tAddress: %s\t\tCourse: %s",aStudent[i].name, aStudent[i].age, aStudent[i].address,aStudent[i].course);
printf("\n");
}
}
void main()
{
struct Students aStudent[2];
int option;
char choice = 'Y';
printf("\t...Welcome to the Student Data System...\n\n");
while(choice == 'Y' || choice == 'y')
{
printf("\nPlease Select An Option: \n1. Input Student Data\n2. View Student Data\n3. Exit Syatem\n\n");
scanf("%d",&option);
switch(option)
{
case 1:
printf("Enter Student Details:\n");
capture(aStudent);
printf("Return to main menu? (Y/N) :");
scanf(" %c",&choice);
break;
case 2:
printf("\nDisplaying Information:\n");
display(aStudent);
printf("Return to main menu? (Y/N) :");
scanf(" %c",&choice);
break;
case 3:
close();
break;
default:
printf("\nSorry, your option is not valid.");
}
}
}
You have not initialized your string instead you have initialized a character! char name, address, course; If you have used 'char *name, *address, *course;' or char name[100], address[100], course[100]; you might have got the answer ! If you use the above case you should scan using scanf("%s",name); ! Hope I answered your question !

writing in a file using structures

I wrote a code that is supposed to write into a file but when i execute the program it says "error in saving student data".Here is the code.
#include<stdio.h>
typedef struct Student{
int numberOfStudents;
char name; // onoma foithth
char surname; // epi8eto foithth
};
int main(){
struct Student s1;
FILE *file=fopen("d:\\student.txt","w");
if(file==NULL){
printf("error in saving student data");
return 1;
}
while(1){
printf("Enter number of students: ");
scanf("%d",&s1.numberOfStudents);
printf("enter name: ");
scanf("%s",&s1.name);
printf("enter surname: ");
scanf("%s",&s1.surname);
fprintf(file,"%d\t%s\t%s\n",s1.numberOfStudents,s1.name,s1.surname);
printf("continue (Y/N)");
char ch=getch();
if (ch=='N' || ch=='n')
break;
}
fclose(file);
return 0;
}
I have searched but i can't find the problem.where is my mistake?
name field of structure should be char array if you want to store onoma foithth in that as shown. modify your structure as
typedef struct Student{
int numberOfStudents;
char name[10]; // onoma foithth
char surname; // epi8eto foithth
};
and then while scanning name remove & because name is itself address.
scanf("%s",s1.name);
Make sure you gave correct path in fopen()

How to write structure members at once in a file using fwrite() function?

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student{
char *name;
char *addr;
int age;
int clas;
}*stu;
int main()
{
FILE *fp;
int choice,another;
size_t recsize;
size_t length;
struct student *stu=(struct student *)malloc(sizeof(struct student));
stu->name=(char *) malloc(sizeof(char)*20);
stu->addr=(char*)malloc(sizeof(char)*20);
recsize=sizeof(*stu);
fp=fopen("student.txt","a+");
if(fp==NULL)
{
fp=fopen("student.txt","w+");
if(fp==NULL)
{
printf("cannot open the file");
exit(1);
}
}
do
{
fseek(fp,1,SEEK_END);
printf("Please Enter student Details\n");
printf("Student Name: ");
scanf("%s",stu->name);
printf("Address: ");
scanf("%s",stu->addr);
printf("Class: ");
scanf("%s",&stu->clas);
printf("Age: ");
scanf("%s",&stu->age);
fwrite(stu,recsize,1,fp);
printf("Add another Enter 1 ?\n");
scanf("%d",&another);
}while(another==1);
fclose(fp);
free(stu);
}
I have code in C which has a structure Student. I am trying to get all structure members values from user. Memory is allocated for structure and two members *name and *addr. when I try to write those values using fwrite() function in a file Student.txt it shows random output ( ཀའ㌱䔀8䵁ཀའ㈱䔀1䵁 ) like this in a file and it is not in readable form. Please provide me the best way to write members of structure in file using fwrite() function.
You need to use %d instead of %s for ints
printf("Class: ");
scanf("%s",&stu->clas);
printf("Age: ");
scanf("%s",&stu->age);
should be
printf("Class: ");
scanf("%d",&stu->clas);
printf("Age: ");
scanf("%d",&stu->age);
And as pointed out by #David Hoelzer in comments: you're writing the value of the pointers rather than what they contain, change
struct student{
char *name;
char *addr;
to
struct student{
char name[20];
char addr[20];
and delete those lines:
stu->name=(char *) malloc(sizeof(char)*20);
stu->addr=(char*) malloc(sizeof(char)*20);

Resources