C Programming and Structures
Results 1 to 3 of 3

Thread: C Programming and Structures

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    C Programming and Structures

    Following is the C code with use of pointers and structures.

    Here i want to fill a struct array of employees and then print the details of that array, sort the contents of that array alphabetically and swap them around.

    Having trouble with pointer passing to the function.

    Please reply asap.


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    struct employeeRecord
    {
    char surname[20];
    double hourlyRate;
    int empNumber;
    };

    typedef struct employeeRecord employee;

    void fillEmployee(employee *emp, char name, double rate, int num);
    void swap(employee employees[], int a, int b);
    void sortEmployees(employee employees[], int number);
    void printEmployees(employee employees[], int number);



    int main()
    {
    employee employees[5];


    fillEmployee(&employees[0], "Cogswell", 35.75, 43);
    fillEmployee(&employees[1], "Alroy", 12.00, 163);
    fillEmployee(&employees[2], "Jetson", 15.75, 97);
    fillEmployee(&employees[3], "Astro", 10.25, 104);
    fillEmployee(&employees[4], "Sprocket", 22.75, 15);

    printEmployees(employees, 5);
    sortEmployees(employees, 5);
    printEmployees(employees, 5);

    return 0;
    }

    void printEmployees(employee employees[], int number)
    {
    int i;

    for (i=0; i<number; i++)
    {
    printf("%-20s%4d %.2f\n", employees[i].surname,
    employees[i].empNumber, employees[i].hourlyRate);
    }
    printf("\n");
    }

    void swap(employee employees[], int a, int b)
    {
    /* code to swap employee a and b around in employees */

    struct employee temp[5];

    employees[a] = temp[0];
    employees[b] = employees[a];
    temp[0] = employees[b];

    }


    void sortEmployees(employee employees[], int number)
    {
    /* use insertion sort to order employees,
    in employee name order
    */

    int inner, outer;

    for (outer=1; outer<number; outer++)
    {
    inner = outer;
    while (inner > 0)
    {
    if (strcmp(employees[inner].surname, employees[inner-1].surname)
    {
    swap(employees, inner, inner-1);
    inner++;
    }
    else break;
    }
    }
    }
    U get What U pay for.

  2. #2
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    665
    Hi

    Just gave it a look ............a few things that caught my eye


    You have made a structure named employeeRecord...............and in the rest of the program you are refering it to as employee

    e.g
    Code:
    
    int main()
    {
          employee employees[5];  <-------- here
    
          Void sortEmployees(employee employees[], int number) <------- here
    
    and everywhere

    [edit] Sorry i am blind employee is ok .........didn't see this line typedef struct employeeRecord employee; [/edit]



    Spelling mistakes and a few forgotton braces like here :
    Code:
    
    if (strcmp(employees[inner].surname, employees[inner-1].surname)  --> ')'
    
    Could you first give it a serious read first


    and there is no function fillEmployee................ errrrrr was that the question how to make fillEmployee function ..................so what's the problem you are facing in it can you be specific

    and please please use code tags the code without it looks so jumbled up ............it's just isen't plesent to read .

    --Good luck--

    [edit]

    Solved your Original problem Created new ones..............giving you something to be busy with...............have you ever heard of error handling you code has lot of what we call silly errors .................works on TC , VC++ , Quick C ......might have problem on others because some of the functions i use are not standard ANSI functions

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct employeeRecord
    {
    	char surname[20];
    	double hourlyRate;
    	int empNumber;
    };
    
    //typedef struct employeeRecord employee;
    
    void fillEmployee(struct employeeRecord *emp, char name[], double rate, int num);
    void swap(struct employeeRecord employees[], int a, int b);
    void sortEmployees(struct employeeRecord employees[], int number);
    void printEmployees(struct employeeRecord employees[], int number);
    
    
    
    int main()
    {
    
    	struct employeeRecord employees[5];
    	clrscr();
    
    	fillEmployee(&employees[0], "Cogswell", 35.75, 43);
    	fillEmployee(&employees[1], "Alroy", 12.00, 163);
    	fillEmployee(&employees[2], "Jetson", 15.75, 97);
    	fillEmployee(&employees[3], "Astro", 10.25, 104);
    	fillEmployee(&employees[4], "Sprocket", 22.75, 15);
    
    	printEmployees(employees, 5);
    	//sortEmployees(employees, 5);
    	//printEmployees(employees, 5);
    
    	getch();
    }
    
    
    void printEmployees(struct employeeRecord employees[], int number)
    {
    	int i;
    
    	for (i=0; i<number; i++)
    	{
    		printf("%-20s%4d %.2f\n", employees[i].surname,
    		employees[i].empNumber, employees[i].hourlyRate);
    	}
    
    	printf("\n");
    }
    
    void swap(struct employeeRecord employees[], int a, int c)
    {
    /* code to swap employee a and b around in employees */
    
    	struct employeeRecord temp[5];
    
    	employees[a] = temp[0];
    	employees[c] = employees[a];
    	temp[0] = employees[c];
    
    }
    
    
    void sortEmployees(struct employeeRecord employees[], int number)
    {
    /* use insertion sort to order employees,
    in employee name order
    */
    
    	int inner, outer;
    
    	for (outer=1; outer<number; outer++)
    	{
    		inner = outer;
    		while (inner > 0)
    		{
    			if(strcmp(employees[inner].surname, employees[inner-1].surname))
    			{
    				swap(employees, inner, inner-1);
    				inner++;
    			}
    		else break;
    		}
    	}
    }
    
    void fillEmployee(struct employeeRecord *emp, char name[], double rate, int num)
    {
    	//emp=&employees;
    
    	strcpy(emp->surname,name);
    	emp->hourlyRate=rate;
    	emp->empNumber=num;
    }

  3. #3
    Senior Member
    Join Date
    Jul 2003
    Posts
    813
    "Having problems" isn't a descriptive description :|

    But I assume it won't compile? The problem I see is referrencing individual array elements, instead of &employees + 2 or something similar. In your case I don't understand why you absolutely want to referrence instead of call... but so far, that's what I recommend... of course I'm a bit rusty on my C
    /\\

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •