I need use pointers for this code. No arrays, no structures. For example, if I n
ID: 3691049 • Letter: I
Question
I need use pointers for this code. No arrays, no structures. For example, if I need an array of floats to hold the scores, do not use float score[15]. Rather use float *score, then use dynamic memory allocation to hold required memory. I must use memory optimally, that is if you have only 6 scores, I need to use to point to a memory chunk of sizeof(float)*6 bytes. Similarly, to hold a name, instead of doing it with 2D char arrays, use 2D pointers (char firstName[15][20] char **firstName).
I have the code in C below, but i'm using arrays. and needed help converting it to only using2d pointers.
Thank you in advance
---------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
void Option_Scren();
void print_records(int size, char firstname[][20], char lastname[][20], float score[]);
void search_by_firstname(int size, char firstname[][20], char lastname[][20], float score[]);
void search_by_lastname(int size, char firstname[][20], char lastname[][20], float score[]);
void sort_by_score(int size, char firstname[][20], char lastname[][20], float score[]);
void sort_by_lastname(int size, char firstname[][20], char lastname[][20], float score[]);
int main(void)
{
char First_Name[15][20];
char Last_Name[15][20];
float Score_[15];
int n = 0;
int i = 0;
int x = -1;
char nametofind[20];
printf("Please indicate the number of records you want to enter (min 5, max 15): ");
scanf("%d", &n);
printf("Please input records of students (enter a new line after each record), with the following format first name last name score ");
for (i = 0; i<n; i++)
{
scanf("%s %s %f", &First_Name[i][0], &Last_Name[i][0], &Score_[i]);
}
do
{
printf(" ---you may access the records by--- ");
printf("Print records (press 1) ");
printf("Search by first name (press 2) ");
printf("Search by last name (press 3) ");
printf("Sort by score (press 4) ");
printf("Sort by last name (press 5) ");
printf("Exit program (press 0) ");
printf("-------------------------- ");
printf("Please a function by entering a value from 0 to 5. ");
scanf("%d", &x);
printf("You have selected option: %d ", x);
switch (x)
{
case 1:
print_records(n, First_Name, Last_Name, Score_);
break;
case 2:
search_by_firstname(n, First_Name, Last_Name, Score_);
break;
case 3: search_by_lastname(n, First_Name, Last_Name, Score_);
break;
case 4: sort_by_score(n, First_Name, Last_Name, Score_);
break;
case 5: sort_by_lastname(n, First_Name, Last_Name, Score_);
break;
default: printf("Please only enter a number from 0 to 5 ");
}
}
while (x != 0);
printf("Thank You Good Bye ");
return 0;
}
void print_records(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
for (i = 0; i < size; i++)
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
void search_by_firstname(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
char Name_Search[20];
printf("Please enter the first name of student record you wish to print: ");
gets(Name_Search);
for (i = 0; i<size; i++)
{
if (strcmp(Name_Search, firstname[i]) == 0)
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
}
void search_by_lastname(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
char Name_Search[20];
printf("Please enter last name of student record you wish to print: ");
gets(Name_Search);
for (i = 0; i<size; i++)
{
if (strcmp(Name_Search, lastname[i]) == 0)
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
}
void sort_by_score(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
int j = 0;
float tempscore;
char tempfirstname[20];
char templastname[20];
// bubble sort
for (i = 0; i<size - 1; i++)
{
for (j = 0; j<size - i - 1; j++)
{
if (score[j]>score[j + 1])
{
tempscore = score[j];
score[j] = score[j + 1];
score[j + 1] = tempscore;
strcpy(tempfirstname, firstname[j]);
strcpy(firstname[j], firstname[j + 1]);
strcpy(firstname[j + 1], tempfirstname);
strcpy(templastname, lastname[j]);
strcpy(lastname[j], lastname[j + 1]);
strcpy(lastname[j + 1], templastname);
}
}
}
print_records(size, firstname, lastname, score);
}
void sort_by_lastname(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
int j = 0;
float tempscore;
char tempfirstname[20];
char templastname[20];
// bubble sort
for (i = 0; i<size - 1; i++)
{
for (j = 0; j<size - i - 1; j++)
{
if (strcmp(lastname[j], lastname[j + 1])>0)
{
strcpy(tempfirstname, firstname[j]);
strcpy(firstname[j], firstname[j + 1]);
strcpy(firstname[j + 1], tempfirstname);
strcpy(templastname, lastname[j]);
strcpy(lastname[j], lastname[j + 1]);
strcpy(lastname[j + 1], templastname);
tempscore = score[j];
score[j] = score[j + 1];
score[j + 1] = tempscore;
}
}
}
print_records(size, firstname, lastname, score);
}
Explanation / Answer
Hi I have replaced arrays with Pointer. Please test it.
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
void Option_Scren();
void print_records(int size, char **firstname, char **lastname, float *score);
void search_by_firstname(int size, char **firstname, char **lastname, float *score);
void search_by_lastname(int size, char **firstname, char **lastname, float *score);
void sort_by_score(int size, char **firstname, char **lastname, float *score);
void sort_by_lastname(int size, char **firstname, char **lastname, float *score);
int main(void)
{
char **First_Name;
char **Last_Name;
float *Score_;
int n = 0;
int i = 0;
int x = -1;
First_Name = (char **)malloc(sizeof(char *));
Last_Name = (char **)malloc(sizeof(char *));
Score_ = (float *)malloc(sizeof(int)*15);
for(i=0; i<15; i++){
First_Name[i] = (char *)malloc(20*sizeof(char));
Last_Name[i] = (char *)malloc(20*sizeof(char));
}
//char *nametofind = (char *)malloc(20*sizeof(char));
printf("Please indicate the number of records you want to enter (min 5, max 15): ");
scanf("%d", &n);
printf("Please input records of students (enter a new line after each record), with the following format first name last name score ");
for (i = 0; i<n; i++)
{
scanf("%s %s %f", &First_Name[i][0], &Last_Name[i][0], &Score_[i]);
}
do
{
printf(" ---you may access the records by--- ");
printf("Print records (press 1) ");
printf("Search by first name (press 2) ");
printf("Search by last name (press 3) ");
printf("Sort by score (press 4) ");
printf("Sort by last name (press 5) ");
printf("Exit program (press 0) ");
printf("-------------------------- ");
printf("Please a function by entering a value from 0 to 5. ");
scanf("%d", &x);
printf("You have selected option: %d ", x);
switch (x)
{
case 1:
print_records(n, First_Name, Last_Name, Score_);
break;
case 2:
search_by_firstname(n, First_Name, Last_Name, Score_);
break;
case 3: search_by_lastname(n, First_Name, Last_Name, Score_);
break;
case 4: sort_by_score(n, First_Name, Last_Name, Score_);
break;
case 5: sort_by_lastname(n, First_Name, Last_Name, Score_);
break;
default: printf("Please only enter a number from 0 to 5 ");
}
}
while (x != 0);
printf("Thank You Good Bye ");
return 0;
}
void print_records(int size, char **firstname, char **lastname, float *score)
{
int i = 0;
for (i = 0; i < size; i++)
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
void search_by_firstname(int size, char **firstname, char **lastname, float *score)
{
int i = 0;
char *Name_Search = (char *)malloc(20*sizeof(char));
printf("Please enter the first name of student record you wish to print: ");
fgets(Name_Search, 20, stdin);
for (i = 0; i<size; i++)
{
if (strcmp(Name_Search, firstname[i]) == 0)
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
}
void search_by_lastname(int size, char **firstname, char **lastname, float *score)
{
int i = 0;
char *Name_Search = (char *)malloc(20*sizeof(char));
printf("Please enter last name of student record you wish to print: ");
fgets(Name_Search, 20, stdin);
for (i = 0; i<size; i++)
{
if (strcmp(Name_Search, lastname[i]) == 0)
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
}
void sort_by_score(int size, char **firstname, char **lastname, float *score)
{
int i = 0;
int j = 0;
float tempscore;
char tempfirstname[20];
char templastname[20];
// bubble sort
for (i = 0; i<size - 1; i++)
{
for (j = 0; j<size - i - 1; j++)
{
if (score[j]>score[j + 1])
{
tempscore = score[j];
score[j] = score[j + 1];
score[j + 1] = tempscore;
strcpy(tempfirstname, firstname[j]);
strcpy(firstname[j], firstname[j + 1]);
strcpy(firstname[j + 1], tempfirstname);
strcpy(templastname, lastname[j]);
strcpy(lastname[j], lastname[j + 1]);
strcpy(lastname[j + 1], templastname);
}
}
}
print_records(size, firstname, lastname, score);
}
void sort_by_lastname(int size, char **firstname, char **lastname, float *score)
{
int i = 0;
int j = 0;
float tempscore;
char tempfirstname[20];
char templastname[20];
// bubble sort
for (i = 0; i<size - 1; i++)
{
for (j = 0; j<size - i - 1; j++)
{
if (strcmp(lastname[j], lastname[j + 1])>0)
{
strcpy(tempfirstname, firstname[j]);
strcpy(firstname[j], firstname[j + 1]);
strcpy(firstname[j + 1], tempfirstname);
strcpy(templastname, lastname[j]);
strcpy(lastname[j], lastname[j + 1]);
strcpy(lastname[j + 1], templastname);
tempscore = score[j];
score[j] = score[j + 1];
score[j + 1] = tempscore;
}
}
}
print_records(size, firstname, lastname, score);
}