Resolved the issues on my own, using different methods than those suggested below! :)
Thanks for viewing my question! :)
I've been learning about structs and working on a practice lab in C, and my code does not seem to be compiling correctly with any change I make to it. Currently I am not receiving any output and the program crashes. I'm still very confused on how to correctly utilize the '*' and '&' symbols when passing them into functions as well. My goals for this practice are to:
print the contents of the array in the same format as the data file
print the full name of the student with the best GPA
calculate and print the average GPA
print the names of all students with GPAs above the average
print the name of the youngest student who has a GPA below average
sort the structures in the array into order from lowest to highest
GPA
print the array again (will now be in a different order from last
time)
How do I properly call to and print these items from the student struct? And how would I access the gpa values to pass into a function that would calculate the average?
#include <stdio.h>
#include <stdlib.h>
// define constants
#define ARR 100
#define FIRST 7
#define MIDINIT 1
#define LAST 9
#define STREET 16
#define CITY 11
#define STATE 2
#define ZIP 5
#define AGE 3
#define GPA 4
#define START 0
#define FIRSTID 8
#define INITID 10
#define STREETID 20
#define CITYID 37
#define STATEID 49
#define ZIPID 52
#define AGEID 57
#define GPAID 64
// defined structs
typedef struct {
char street[STREET + 1];
char city[CITY + 1];
char state[STATE + 1];
char zip[ZIP + 1];
} Address;
typedef struct {
char firstname[FIRST + 1];
char initial[MIDINIT + 1];
char lastname[LAST + 1];
Address ofstudent;
int age;
double gpa;
} Student;
// function prototype
void strsub(char buf[], char s[], int start, int size);
void processStudent(int *id, Student students[]);
void sortStudentGpa(Student *students, int id);
void maxGpa(Student *students, int id);
/* lab6student.c: creates an array of student structures and outputs reports */
int main(void)
{
Student students[ARR]; // creates an array of student structures
int id = 0; // counter for student
processStudent(&id, students);
maxGpa(students, id);
}
void strsub(char buf[], char s[], int start, int size) {
int i;
for (i = 0; i < size && buf[start + i] != '\0'; i++) {
// loops as long as iterator is less than size
// and while string has not run out of characters
s[i] = buf[i + start];
}
s[i] = '\0';
}
/* void sort(Student *students, int id) {
int j, i;
for(i = 1; i < n; i++) {
for(j = 0; j < id - i; j++) {
if(students[j].gpa > students[j + 1].gpa) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
} */
void processStudent(int *id, Student students[]) {
FILE *data;
char line[ARR];
*id = 0; // counter for student
data = fopen("Students.dat", "r");
if (data == NULL) {
printf("Students.dat file not found!\n");
exit(1);
}
// process file
while (!feof(data)) {
// organize student info into separate arrays
fgets(line, ARR, data);
strsub(line, students[*id].firstname, START, FIRST);
strsub(line, students[*id].initial, FIRSTID, MIDINIT);
strsub(line, students[*id].lastname, INITID, LAST);
strsub(line, students[*id].ofstudent.street, STREETID, STREET);
strsub(line, students[*id].ofstudent.city, CITYID, CITY);
strsub(line, students[*id].ofstudent.state, STATEID, STATE);
strsub(line, students[*id].ofstudent.zip, ZIPID, ZIP);
students[*id].age = atoi(&line[AGEID]);
students[*id].gpa = atoi(&line[GPAID]);
(*id)++;
}
fclose(data);
}
//sorts struct student array containing num (gpa) elements into
//ascending order
void sortStudentGpa(Student *students, int id) {
int i, j; // indexes into unsorted and sorted partitions
Student temp; // temporarily holds an element from the array
for (i = 1; i < id; ++i) {
temp = students[i];
j = i - 1;
while (j >= 0 && temp.gpa < students[j].gpa) {
students[j + 1] = students[j];
j = j - 1;
}
students[j + 1] = temp;
}
}
void maxGpa(Student *students, int id) {
int iwithmax, i;
float max = 0;
for ( i = 0 ; i < id ; i++) {
if (students -> gpa > max) {
max = students -> gpa;
iwithmax = i;
}
}
printf("\n\nHighest GPA is done by Student %d with GPA = %f", iwithmax, max);
}
In the maxGpa function just change the definition as
void maxGpa(Student *students, int id);
then in the maxGpa function do the following changes
void maxGpa(Student *students, int id) {
int iwithmax, i;
float max = 0;
for ( i = 0 ; i < id ; i++) {
if (students -> gpa > max) {
max = students -> gpa;
iwithmax = i;
}
}
Try this.....
Related
I need to sort students according to their surname or if their surname is the same according to their name in lexicographical order.
#include <stdio.h>
struct Student {
char name[20], surname[20];
};
void sort(struct Student students[], int n) {
int i, j, temp;
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
if (students[j].surname > students[i].surname ||
students[j].name > students[i].name) {
temp = i;
students[i] = students[j];
students[j] = students[temp];
}
}
void main() {
struct Student students[6] = {
{"Mujo", "Mujic"},
{"Meho", "Mujic"},
{"Pero", "Peric"},
{"Beba", "Bebic"},
{"Mujo", "Mujic"},
{"Fata", "Fatic"},
};
sort(students, 6);
int i;
for (i = 0; i < 6; i++)
printf("%s %s\n", students[i].surname, students[i].name);
}
This prints only one student six times. Could you help me to fix this?
Note: using auxiliary arrays is not allowed
Your swap code is dubious — broken, I believe. A big warning bell is the type of temp — it needs to be a struct Student.
You have:
temp = i;
students[i] = students[j];
students[j] = students[temp];
You need:
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
Obviously, remove the definition int temp; as well.
However, there are other problems too (as ever). You can't usefully compare strings using relational operators — use strcmp(). And your ordering test is wrong too, even when revised to use strcmp().
This code does the job, sorting names in descending order:
#include <stdio.h>
#include <string.h>
struct Student
{
char name[20];
char surname[20];
};
static void sort(struct Student students[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
int rc = strcmp(students[j].surname, students[i].surname);
if (rc > 0 ||
(rc == 0 && strcmp(students[j].name, students[i].name) > 0))
{
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
static void dump_array(const char *tag, size_t size, const struct Student *students)
{
printf("%s (%zu):\n", tag, size);
for (size_t i = 0; i < size; i++)
printf("%s %s\n", students[i].surname, students[i].name);
}
int main(void)
{
struct Student students[] =
{
{"Mujo", "Mujic"},
{"Meho", "Mujic"},
{"Pero", "Peric"},
{"Zebra", "Elephant"},
{"Beba", "Bebic"},
{"Mujo", "Mujic"},
{"Abelone", "Shells"},
{"Fata", "Fatic"},
};
enum { NUM_STUDENTS = sizeof(students) / sizeof(students[0]) };
dump_array("Before", NUM_STUDENTS, students);
sort(students, NUM_STUDENTS);
dump_array("After", NUM_STUDENTS, students);
return 0;
}
Output:
Before (8):
Mujic Mujo
Mujic Meho
Peric Pero
Elephant Zebra
Bebic Beba
Mujic Mujo
Shells Abelone
Fatic Fata
After (8):
Shells Abelone
Peric Pero
Mujic Mujo
Mujic Mujo
Mujic Meho
Fatic Fata
Elephant Zebra
Bebic Beba
In C i'm creating a grade system and Im stuck on how to get the weighed grade of homework and tests separately then add them to an overall grade. There are 10 hw and 5 test with each hw being 5% of the overall grade and each test being 10% of the overall grade making 100% total. In my code I randomly generate each test and hw grade now I want to add up hw and test scores separately, find the weighted grade for both categories, then add them to show overall weighted grade. Im stuck on getting the sums for each test and where to put that in my code.
here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ID_LEN 10
#define NAME_LEN 21
#define HW_SCORES 10
#define TEST_SCORES 5
typedef struct {
char id[ID_LEN];
char fname[NAME_LEN];
char lname[NAME_LEN];
char mi;
unsigned char hw_scores[HW_SCORES];
unsigned char test_scores[TEST_SCORES];
//modify struct stu_rect_type
} stu_rec_type;
int idcmp(const void *a, const void *b) {
stu_rec_type *pa = (stu_rec_type *) a;
stu_rec_type *pb = (stu_rec_type *) b;
return -(strcmp(pa->id, pb->id));
}
int lnamecmp(const void *a, const void *b) {
stu_rec_type *pa = (stu_rec_type *) a;
stu_rec_type *pb = (stu_rec_type *) b;
return -(strcmp(pa->lname, pb->lname));
}
int scorecmp(const void *a, const void *b) {
stu_rec_type *pa = (stu_rec_type *) a;
stu_rec_type *pb = (stu_rec_type *) b;
int total_a = 0, total_b = 0;
int i;
for (i=0; i < HW_SCORES; i++) {
total_a += 0.1*pa->hw_scores[i];
total_b += 0*1*pb->hw_scores[i];
}
for (i=0; i < TEST_SCORES; i++) {
total_a += 0.05*pa->test_scores[i];
total_b += 0.05*pb->test_scores[i];
}
if (total_a == total_b)
return strcmp(pa->lname, pb->lname);
//Sorting in descending order
else if(total_a > total_b)
return -1;
else
return 1;
}
void print_rec(stu_rec_type *rec_ptr) {
int j;
printf("ID: %s\n", rec_ptr->id);
printf("NAME: %s %c. %s\n", rec_ptr->fname, rec_ptr->mi, rec_ptr->lname );
printf("HW SCORES:\n");
for (j=0; j <HW_SCORES; j++){
printf("%4d", rec_ptr->hw_scores[j]);
if ( (j+1)%10 ==0)
printf("\n");
}
printf("TEST SCORES:\n");
for (j=0; j <TEST_SCORES; j++){
printf("%4d", rec_ptr->test_scores[j]);
if ( (j+1)%10 ==0)
printf("\n");
}
}
int main(){
//prompt user to input number of students
int CLASS_SIZE;
printf("Enter the number of students : ");
scanf("%d",&CLASS_SIZE);
stu_rec_type *cs3500_class;
//malloc to allocate momeory of student records
// Dynamically allocating memory for an array of student records.
cs3500_class=(stu_rec_type *)malloc(sizeof(stu_rec_type)*CLASS_SIZE);
int i, j, k;
char *ptr;
stu_rec_type *stu_ptr;
int n = 700000000;
char id[10];
srand(time(NULL));
for(i = 0; i < CLASS_SIZE; i++) {
// generate ids
//ptr = (char *) &(cs3500_class[i].id);
// sprintf(ptr, "%i", ++n);
sprintf((char *)&(cs3500_class[i].id), "%i", ++n);
// generate the first names
cs3500_class[i].fname[0] = 'A' + rand()%26;
k = rand()%(NAME_LEN-1);
for (j=0; j < k; j++)
cs3500_class[i].fname[j+1] = 'a' + rand()%26;
cs3500_class[i].fname[k+1] = '\0';
// generate the last names
cs3500_class[i].lname[0] = 'A' + rand()%26;
k = rand()%(NAME_LEN-1);
for (j=0; j < k; j++)
cs3500_class[i].lname[j+1] = 'a' + rand()%26;
cs3500_class[i].lname[k+1] = '\0';
// generate mi
cs3500_class[i].mi = 'A' + rand()%26;
// generate hw scores between 10-20 max being 20
for (j=0; j<HW_SCORES; j++)
cs3500_class[i].hw_scores[j] = (rand() % 10) + 11;
// generate test scores between 50-100 max being 100
for (j=0; j<TEST_SCORES; j++)
cs3500_class[i].test_scores[j] = 51 + (rand()%50);
}
// displaying the first 5 records
for (i = 0; i < CLASS_SIZE; i++) {
print_rec(&cs3500_class[i]);
printf("\n\n ");
}
// sort the records based on the 1st score
//using qsort() in descending order
printf("\n\nAfter qsort\n");
qsort(cs3500_class, CLASS_SIZE, sizeof(stu_rec_type), scorecmp);
// display the first 5 records
for (i = 0; i < CLASS_SIZE; i++) {
print_rec(&cs3500_class[i]);
printf("\n\n");
}
return 0;
Here is the output as of now
I plan to show the grades in order of the ID number then after qsort I will show in order of highest grade.
Here is the task i'm working on:
I am given a txt file containing the list of student names, id numbers, schools, majors, and test scores.
Read this contents and copy to the structure in C.
Sort this list using insertion sort.
Print sorted list on the screen.
I checked my coding by muting some parts, there is an error with my insertion sorting function.
I have no idea which part is incorrect. It all makes sense to me. I need help :( here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000
#define BUF_SIZE 80
typedef struct {
char name[20];
char studentID[10];
char department[20];
char major[20];
int mid;
int final;
} student;
FILE *fp;
void operation(student t, student list[], int j);
void insertion_sort(student list[], int n);
void printing(student list[], int n);
int main(int argc, char *argv[])
{
char filename[20] = "studentlist.txt";
int n = 1; /* (number of students) + 1 */
student list[MAX];
char buffer[BUF_SIZE];
int i;
fp = fopen(filename, "r");
while (1) {
if (fgets(buffer, BUF_SIZE, fp) == NULL)
break;
strncpy(list[n].name, buffer, strlen(buffer) - 1);
fgets(buffer, BUF_SIZE, fp);
strncpy(list[n].studentID, buffer, strlen(buffer) - 1);
fgets(buffer, BUF_SIZE, fp);
strncpy(list[n].department, buffer, strlen(buffer) - 1);
fgets(buffer, BUF_SIZE, fp);
strncpy(list[n].major, buffer, strlen(buffer) - 1);
fgets(buffer, BUF_SIZE, fp);
list[n].mid = atoi(buffer);
fgets(buffer, BUF_SIZE, fp);
list[n].final = atoi(buffer);
n++;
}
fclose(fp);
insertion_sort(list, n);
printing(list, n);
return 0;
}
void insertion_sort(student list[], int n)
{
int i;
student temp;
for (i = 2; i < n; i++) {
temp = list[i];
operation(temp, list, i - 1);
}
}
void operation(student t, student list[], int j)
{
list[0] = t;
while (t.studentID < list[j].studentID) {
list[j + 1] = list[j];
j--;
}
list[j + 1] = t;
}
void printing(student list[], int n)
{
int i;
for (i = 1; i < n; i++) {
printf(" %s ", list[i].name);
printf(" %s ", list[i].studentID);
printf(" %s ", list[i].department);
printf(" %s ", list[i].major);
printf(" %6d ", list[i].mid);
printf(" %6d ", list[i].final);
putchar('\n');
}
}
Continuing from the comments and the first answer, you are making tracking index error between the split insertion_sort and operation functions. There is no need for two functions when one will work (and arguably be smaller). There is little benefit to be obtained from the split (except for increased confusion).
Putting a simple insertion sort together in one function that will fit your needs (and using pointers to sort as opposed to repeatedly using the function copy-constructor to accomplish the swaps), you could do something similar to the following:
typedef struct {
int id, g;
} student;
void insertion_sort (student **list, int nmemb)
{
for (int i = 0; i < nmemb; i++)
for (int j = i; j > 0 && list[j]->id < list[j-1]->id; j--)
{
student *tmp = list[j];
list[j] = list[j-1];
list[j-1] = tmp;
}
}
Putting together a short (and limited struct member example), you can do something like the following to sort the student data by ID (or id below)
#include <stdio.h>
typedef struct {
int id, g;
} student;
void insertion_sort (student **list, int nmemb)
{
for (int i = 0; i < nmemb; i++)
for (int j = i; j > 0 && list[j]->id < list[j-1]->id; j--)
{
student *tmp = list[j];
list[j] = list[j-1];
list[j-1] = tmp;
}
}
void printing(student *list[], int n)
{
int i;
for (i = 0; i < n; i++) {
printf(" %d ", list[i]->id);
printf(" %d \n", list[i]->g);
}
}
int main (void) {
student s[] = { {.id = 5, .g = 72}, /* minimal student test data */
{.id = 2, .g = 91},
{.id = 4, .g = 77},
{.id = 1, .g = 96},
{.id = 3, .g = 85}};
int n = sizeof s / sizeof *s;
student *l[n];
for (int i = 0; i < n; i++) /* initialize pointers in l */
l[i] = &s[i];
insertion_sort (l, n); /* sort pointers in l */
printing (l, n); /* output sorted pointers */
return 0;
}
Example Use/Output
Sorted by student.id
$ ./bin/inssort_so
1 96
2 91
3 85
4 77
5 72
If you did want to avoid using the additional level of indirection involved in passing an array of pointers (actually a pointer-to-pointer-to-type), you can make your original approach work as follows:
void insertion_sort (student *list, int nmemb)
{
for (int i = 0; i < nmemb; i++)
for (int j = i; j > 0 && list[j].id < list[j-1].id; j--)
{
student tmp = list[j];
list[j] = list[j-1];
list[j-1] = tmp;
}
}
void printing (student *list, int n)
{
for (int i = 0; i < n; i++)
printf (" %d %d \n", list[i].id, list[i].g);
}
int main (void) {
student s[] = { {.id = 5, .g = 72},
{.id = 2, .g = 91},
{.id = 4, .g = 77},
{.id = 1, .g = 96},
{.id = 3, .g = 85}};
int n = sizeof s / sizeof *s;
insertion_sort (s, n);
printing (s, n);
return 0;
}
As you go forward, also consider passing a compare callback function to your sort routines. That allows you to sort many different types of data using your same sort routine -- just by changing the compare function (as is done for the C-library qsort routine.
I am sorting an array of structs using qsort, and I am looking for a more efficient way to extract the top three highest scores by partially sorting the array. My structure looks like this:
typedef struct {
double score;
int player_num;
} player_t;
and I have malloced an array of structures like this:
player_t *players = malloc(SIZE * sizeof(player_t));
The way I sort this array of structures is by first sorting the scores, and if the scores have ties, then sort by the player number.
My code looks like this with qsort:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 6
#define MAX 3
int scorecmp(const void *a, const void *b);
int ComparePlayerScore( const void* ap , const void* bp );
int CompareInt( const int a , const int b );
typedef struct {
double score;
int player_num;
} player_t;
int
main(int argc, char *argv[]) {
int i;
int player_numbers[] = {1, 2, 3, 4, 5, 6};
double scores[] = {0.765, 0.454, 0.454, 0.345, 0.643, 0.532};
player_t *players = malloc(SIZE * sizeof(player_t));
for (i = 0; i < SIZE; i++) {
players[i].score = scores[i];
players[i].player_num = player_numbers[i];
}
qsort(players, SIZE, sizeof(*players), ComparePlayerScore);
for (i = 0; i < MAX; i++) {
printf("Player %d: Score: %.3f\n", players[i].player_num, players[i].score);
}
free(players);
return 0;
}
int ComparePlayerScore( const void* ap , const void* bp ) {
const player_t* const a = ap;
const player_t* const b = bp;
if( a->score == b->score ){
return CompareInt( a->player_num , b->player_num );
}
else if( a->score > b->score ) {
return -1;
}
else {
return 1;
}
}
int CompareInt( const int a , const int b ) {
if( a < b ){
return -1;
}
else if( a > b ){
return 1;
}
return 0;
}
I am just looking for another way to do this, but a more efficient way to extract the top 3 the scores, along with the respective player numbers. Like a way where I can extract the top three elements of the array without having to sort the whole array first everytime.
This is my offering. Since you have #define MAX for how many top scores you want to find, I have considered that.
The program makes a single pass of the data, and 1 pass of the topmost for each of the records. I have used a fixed array, you'll have to adapt to your needs.
#include <stdio.h>
#define SIZE 6
#define MAX 3
typedef struct {
double score;
int player_num;
} player_t;
int main(int argc, char *argv[])
{
player_t players[SIZE] = {{2.0, 2}, {4.0, 5}, {1.0, 4}, {1.0, 1}, {5.0, 3}, {4.0, 6}};
int order[MAX+1] = {0};
int found = 1;
int i, j;
int bigger;
for(i = 1; i < SIZE; i++) {
bigger = 0;
for(j = 0; j < found; j++) {
if(players[i].score > players[ order[j] ].score) {
bigger = 1;
break;
}
else if(players[i].score == players[ order[j] ].score &&
players[i].player_num < players[ order[j] ].player_num) {
bigger = 1;
break;
}
}
if(bigger) {
memmove(order + j + 1, order + j, (found - j) * sizeof order[0]);
order[j] = i;
if(found < MAX) {
found++;
}
}
}
for(i = 0; i < found; i++) {
printf("%d %f\n", players[ order[i] ].player_num, players[ order[i] ].score);
}
return 0;
}
Program output:
3 5.000000
5 4.000000
6 4.000000
Just a simple try, see online demonstration on http://ideone.com/8A1lnP.
struct top3_players {
player_t *top[3];
};
void top3_determine(struct top3_players *top, player_t *players, size_t n) {
player_t *top1 = NULL;
player_t *top2 = NULL;
player_t *top3 = NULL;
for (size_t i = 0; i < n; i++) {
player_t *player = &players[i];
if (top1 == NULL || ComparePlayerScore(player, top1) < 0) {
top3 = top2;
top2 = top1;
top1 = player;
} else if (top2 == NULL || ComparePlayerScore(player, top2) < 0) {
top3 = top2;
top2 = player;
} else if (top3 == NULL || ComparePlayerScore(player, top3) < 0) {
top3 = player;
}
}
top->top[0] = top1;
top->top[1] = top2;
top->top[2] = top3;
}
Here is a solution that keeps track of the top three scores by storing pointers to them. When you add a player or change a score, an update function is called to keep the top-score list current. The advantage here is that you only ever need to iterate over a list of three elements. I modified your original code to demonstrate how this might work:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 6
#define MAX 3
typedef struct {
double score;
int player_num;
} player_t;
void update_topscores(player_t **top, player_t *pplayer);
int
main(int argc, char *argv[]) {
int i;
int player_numbers[] = {1, 2, 3, 4, 5, 6};
double scores[] = {0.765, 0.454, 0.454, 0.345, 0.643, 0.532};
player_t *players = malloc(SIZE * sizeof(player_t));
player_t **topscores = calloc(3, sizeof(player_t *));
for (i = 0; i < SIZE; i++) {
players[i].score = scores[i];
players[i].player_num = player_numbers[i];
update_topscores(topscores, &(players[i]));
}
for (i = 0; i < SIZE; i++) {
printf("Player %d: Score: %.3f\n",
players[i].player_num, players[i].score);
}
puts("Top Scores");
for (i = 0; i < 3; i++) {
printf("Player %d: Score: %.3f\n",
topscores[i]->player_num, topscores[i]->score);
}
/* Change score for player 4 */
players[3].score = 0.755;
update_topscores(topscores, &(players[3]));
puts("New Top Scores");
for (i = 0; i < 3; i++) {
printf("Player %d: Score: %.3f\n",
topscores[i]->player_num, topscores[i]->score);
}
free(players);
free(topscores);
return 0;
}
void update_topscores(player_t **top, player_t *pplayer)
{
int i;
player_t *temp;
for (i = 0; i < 3; i++)
if (top[i] == NULL) {
top[i] = pplayer;
return ;
}
for (i = 0; i < 3; i++) {
if ((pplayer->score) > (top[i]->score)) {
temp = top[i];
top[i] = pplayer;
update_topscores(top, temp);
break;
}
}
return ;
}
You can see that there is a function update_topscores() that is used to update the list. In the above code, this is used when building the initial list of players. Then, when the score of Player 4 is updated, the function is called again, resulting in a new list of top scores. The list is not sorted, but it would be easy to do so if desired, and you would only be sorting a list of three entries.
There was an additional allocation for three pointers to player pointers, topscores, which must of course be freed at the end.
Here is a sample output:
Player 1: Score: 0.765
Player 2: Score: 0.454
Player 3: Score: 0.454
Player 4: Score: 0.345
Player 5: Score: 0.643
Player 6: Score: 0.532
Top Scores
Player 1: Score: 0.765
Player 5: Score: 0.643
Player 6: Score: 0.532
New Top Scores
Player 1: Score: 0.765
Player 4: Score: 0.755
Player 5: Score: 0.643
I want to fill and print out the deck of 52 cards, and then print out 5 hands of 5 non-repeating cards successfully, but then it doesn't work. How would I be able to fix that?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* handy typedefs */
typedef unsigned char card;
typedef unsigned char pairs;
/* arrays for the names of things */
static char *suits[4] = {"Hearts","Diamonds","Clubs","Spades"};
static char *values[13]= {"Ace","Two","Three","Four","Five","Six",/
"Seven","Eight","Nine","Ten","Jack","Queen","King"};
static char *colour[]= {"Black","Red"};
int main()
{
card deck[52][24],*deckp;
int s, c, a;
for (s = 0; s < 4; s++)
{
for(c = 0; c < 13; c++)
{
sprintf(deck[(s * c) + c], "%s of %s", values[c], suits[s]);
}
}
for(a = 0; a < 52; a++)
{
printf("%s\n", deck[a]);
}
int hand,cd,winner;
int iRand;
int i;
int irand;
srand(time(NULL)); /* seed the random number generator */
for(cd=0;cd<5;cd++)
{
for(hand=0;hand<5;hand++)
{
/* deal the hands here */
}
}
for (hand=0;hand<5;hand++)
{
printf("Hand %i:\n",hand+1 );
for ( i = 0; i < 5; i++) {
irand = (rand() % 52);
printf(" %s \n ", deck[irand]);
}
}
/* determine the winner and print it */
return 0;
}
void filldeck(card deck[52])
{
return;
}
void shuffle(card deck[52])
{
int i,rnd;
card c;
for(i=0;i<52;i++)
{
/* generate a random number between 0 & 51 */
rnd=rand() * 52.0 / RAND_MAX;
c = deck[i];
deck[i] = deck[rnd];
deck[rnd] = c;
}
}
Replace deck[(s * c) + c] with deck[s * 13 + c].