GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Feb-2008, 15:40
odin607 odin607 is offline
New Member
 
Join Date: Feb 2008
Posts: 5
odin607 is on a distinguished road

Sorting (structures and arrays fun fun..)


k second semester coding... and i get this lab, its basically done.. but the prof gave us this code to sort our structure or array or w/e

this is C, and i can only use C

CPP / C++ / C Code:
void InsertionSort(int numbers[], int array_size)
{
/*
This is a typical function that sorts the array of integers called numbers into ascending order
using the "Insertion Sort" algorithm. The number of integers in the array is in array_size.
*/
  int i, j, index;

  for (i=1; i < array_size; i++)
  {
    index = numbers[i];
    j = i;
    while ((j > 0) && (numbers[j-1] > index))
    {
      numbers[j] = numbers[j-1];
      j = j - 1;
    }
    numbers[j] = index;
  }
}

obviously this is for arrays, so were suppose to change it so itll sort the our arrays according to the name part of the structure.. youll know what i mean..


CPP / C++ / C Code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {                // = Main Structure =
        char name[26];         //               *
        int midterm[3];          //                   *
        int final;}                 //                   *
STUDENT;                        // ==================

void Process(STUDENT StuData[]);                                        // Processing Function (fills in the
main structure with data from input file)
void Sort(STUDENT numbers[], int array_size);                // Sorting Function (sorts
the main structure data alphabetically)
void Print(STUDENT StuData[], int array_size);                // Printing
Functionv(sorts name part of the main structure withing array StuData)

STUDENT StuData[6];                                // Array containing main structure
char last[13], first[13];                // Arrays to hold names pre-concatenation

void main()
{
        STUDENT StuData[6];                        // Array containing main structure

        printf("Executing processing function . . .");
        Process(StuData);
        printf("Executing sorting function . . .");
        Sort(StuData,6);
        printf("Executing printing function . . .");
        Print(StuData,6);
}

void Process(STUDENT StuData[])
{
        int i;                                                // Counting variable
        FILE *Fi=NULL;                                // File Input

        printf("  Started.\n");
        printf("Opening input file . . .");

        Fi=fopen("student_data.txt","r+t");

        if(Fi == NULL)                                                                        // =File opening status=
        {                                                                                                //                        *
                printf("             Failed!\n");                        //                        *
                exit(1);                                                                        //                        *
        }                                                                                                //                        *
        if(Fi != NULL)                                                                        //                        *
                printf("             Successful!\n");                // =====================

        printf("Processing starting . . .");

        for(i=0;i<6;i++)
        {
                fscanf(Fi,"%s%s%d%d%d%d",last,first,&StuData[i].midterm[0],&StuData[i].midterm[1],&StuData[i].midterm[2],&StuData[i].final);
                strcpy(StuData[i].name,last);
                strcat(StuData[i].name," ");
                strcat(StuData[i].name,first);
        }
        printf("            Finished!\n");
}

void Sort(STUDENT StuData[], int array_size)         // Pre-made Function (edited)
{
        int i, j=0;
        STUDENT index[6];

        for (i=0;i<array_size; i++)
        {
                index[i].name = StuData[i].name;
                j = i;
                while ((j > 0) && (StuData[j-1].name > index[j-1].name))
                {
                        StuData[j].name = StuData[j-1].name;
                        j = j - 1;
                }
                StuData[j].name = index[j].name;
        }
        printf("     Finished!\n");
}

void Print(STUDENT StuData[], int array_size)
{
        int j;                // Counting variable

        printf("    Started!\n\n");

        for(j=0;j<6;j++)
        {
                printf("J=%d: ",j);
                printf("%s: %d %d %d
%d\n",StuData[j].name,StuData[j].midterm[0],StuData[j].midterm[1],StuData[j].midterm[2],StuData[j].final);
                if(j==(array_size-1))
                        printf("\n\n\nPrinting . . .                       Finished!\n");
        }
        printf("Input file anaylsis . . .            Complete.\n\n");
}

comments moved when i sent it to myself but w/e lol

and to clearify, i need to basically just use the first part of coding. i did (changed name to sort), but it doesn't work so i need to know what i need to fix so it actually does sort.

btw as is, it gives me 3 error C2106s ( '=' left operand must be 1-value), 1 at each of the places i say array[w/e].name=array[w/e].name

thanks for reading :O!
  #2  
Old 08-Feb-2008, 16:20
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sorting (structures and arrays fun fun..)


Quote:
Originally Posted by odin607
...second semester coding...
I respectfully suggest that you review the first semester stuff where you dealt with C-style "strings." You know, those functions like strcpy(), and strcmp(). Stuff like that.

As far as your specific error messages:
That element of the struct is an array. In C (C++. too) you can't copy an array with an assignment statement.
I will remind you that the strcpy() function might be appropriate since the arrays hold C-style "strings." See Footnote.

As far as functionality:
I will also remind you that you can't compare contents of an array with another by comparing the names of the arrays.
You might try strcmp() in places whether you want to see whether one "name" is greater than another.

Regards,

Dave

Footnote: The name of an array, used by itself (without the [] brackets) is taken to be a constant pointer to the first element of the array. That's why it is an error to put the name of an array on the left side of an assignment statement. That's also why it is not productive to compare the names of the arrays when you want to compare the contents of the arrays.
  #3  
Old 08-Feb-2008, 16:43
odin607 odin607 is offline
New Member
 
Join Date: Feb 2008
Posts: 5
odin607 is on a distinguished road

Re: Sorting (structures and arrays fun fun..)


well lets assume the rest of the program runs peachy and stuff, and i have tried string commands on it, unless i was just a turdbuglar and messed it up

anyways you mean somin like...

CPP / C++ / C Code:
void InsertionSort(int numbers[], int array_size)
{
/*
This is a typical function that sorts the array of integers called numbers into ascending order
using the "Insertion Sort" algorithm. The number of integers in the array is in array_size.
*/
  int i, j, index;

  for (i=1; i < array_size; i++)
  {
    index = numbers[i];
    j = i;
    while ((j > 0) && (numbers[j-1] > index))
    {
      numbers[j] = numbers[j-1];
      j = j - 1;
    }
    numbers[j] = index;
  }
}

its printing this now (#s representing their position from top to bottom within the input text file)
name1 1 1 1 1
name1 2 2 2 2
name3 3 3 3 3
name3 4 4 4 4
name5 5 5 5 5
name5 6 6 6 6

any ideas =/?
  #4  
Old 08-Feb-2008, 17:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sorting (structures and arrays fun fun..)


Quote:
Originally Posted by odin607
...
its printing this now...

Huh? What's printing what? The second program in your original post didn't compile, and had (at least) one other kind of bug that I mentioned.

The function in your most recent post is the sort program for an array of ints, and doesn't print anything.

It works for my at least one test case:

CPP / C++ / C Code:
#include <stdio.h>

void InsertionSort(int numbers[], int array_size);

int main()
{
    int x[6] = {3, 4, 2, 1, 6, 5};
    int i;
    printf("Initially          : ");
    for (i = 0; i < 6; i++) {
        printf("%d ", x[i]);
    }
    printf("\n");
    InsertionSort(x, 6);
    printf("After InsertionSort: ");
    for (i = 0; i < 6; i++) {
        printf("%d ", x[i]);
    }
    printf("\n");
    return 0;
}

/* The integer InsertionSort from your last post goes here */

Output:
Code:
Initially : 3 4 2 1 6 5 After InsertionSort: 1 2 3 4 5 6

So, what is it that you are testing? What are you asking?

Regards,

Dave

Footnote: Just because it appears to work with that single test case, don't take it to mean that I am certifying that it is OK. You can't prove the program works by testing, and certainly not with just one test case.
  #5  
Old 08-Feb-2008, 17:24
odin607 odin607 is offline
New Member
 
Join Date: Feb 2008
Posts: 5
odin607 is on a distinguished road

Re: Sorting (structures and arrays fun fun..)


i replaced the sort function from my original actual program, with the sort i just posted because thats what i need fixed


heres what it stands at now

CPP / C++ / C Code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {                // = Main Structure =
        char name[26];                //               *
        int midterm[3];                //                   *
        int final;}                        //                   *
STUDENT;                                // ==================

void Process(STUDENT StuData[]);                                        // Processing Function (fills in the
main structure with data from input file)
void Sort(STUDENT numbers[], int array_size);                // Sorting Function (sorts
the main structure data alphabetically)
void Print(STUDENT StuData[], int array_size);                // Printing
Functionv(sorts name part of the main structure withing array StuData)

STUDENT StuData[6];                                // Array containing main structure
char last[13], first[13];                // Arrays to hold names pre-concatenation

void main()
{
        STUDENT StuData[6];                        // Array containing main structure

        printf("Executing processing function . . .");
        Process(StuData);
        printf("Executing sorting function . . .");
        Sort(StuData,6);
        printf("Executing printing function . . .");
        Print(StuData,6);
}

void Process(STUDENT StuData[])
{
        int i;                                                // Counting variable
        FILE *Fi=NULL;                                // File Input

        printf("  Started.\n");
        printf("Opening input file . . .");

        Fi=fopen("student_data.txt","r+t");

        if(Fi == NULL)                                                                        // =File opening status=
        {                                                                                                //                        *
                printf("             Failed!\n");                        //                        *
                exit(1);                                                                        //                        *
        }                                                                                                //                        *
        if(Fi != NULL)                                                                        //                        *
                printf("             Successful!\n");                // =====================

        printf("Processing starting . . .");

        for(i=0;i<6;i++)
        {
                fscanf(Fi,"%s%s%d%d%d%d",last,first,&StuData[i].midterm[0],&StuData[i].midterm[1],&StuData[i].midterm[2],&StuData[i].final);
                strcpy(StuData[i].name,last);
                strcat(StuData[i].name," ");
                strcat(StuData[i].name,first);
        }
        printf("            Finished!\n");
}

void Sort(STUDENT StuData[], int array_size)         // Pre-made Function (edited)
{
        int i, j;
        STUDENT index[6];

        for (i=0;i<array_size; i++)
        {
                strcpy(index[i].name,StuData[i].name);
                j = i;
                while ((j > 0) && (strcmp(StuData[j-1].name,index[j].name)==1))
                {
                        strcpy(StuData[j].name,StuData[j-1].name);
                        j = j - 1;
                }
                strcpy(StuData[j].name,index[j].name);
        }
        printf("     Finished!\n");
}

void Print(STUDENT StuData[], int array_size)
{
        int j;                // Counting variable

        printf("    Started!\n\n");

        for(j=0;j<6;j++)
        {
                printf("J=%d: ",j);
                printf("%s: %d %d %d
%d\n",StuData[j].name,StuData[j].midterm[0],StuData[j].midterm[1],StuData[j].midterm[2],StuData[j].final);
                if(j==(array_size-1))
                        printf("\n\n\nPrinting . . .                       Finished!\n");
        }
        printf("Input file anaylsis . . .            Complete.\n\n");
}

heres the input file:

Cardozzi, Emil 85 94 79 93
Antonucci, Peter 75 91 89 89
Messier, Paul 87 88 89 90
Matsumoto, Carla 78 96 88 91
Pfizer, Lisa 83 79 93 91
Couto, Michael 73 86 92 90

So whats being spit out is..:

Cardozzi, Emil 85 94 79 93
Cardozzi, Emil 75 91 89 89
Messier, Paul 87 88 89 90
Messier, Paul 78 96 88 91
Pfizer, Lisa 83 79 93 91
Pfizer, Lisa 73 86 92 90

sorry about not being crystal clear
  #6  
Old 08-Feb-2008, 17:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sorting (structures and arrays fun fun..)


Quote:
Originally Posted by odin607
i replaced the sort function from my original actual program, with the sort i just posted because thats what i need fixed

Well, you had already posted the integer sort.
Quote:
Originally Posted by odin607
heres what it stands at now

Huh?

CPP / C++ / C Code:
/* from the integer sort */
    while ((j > 0) && (numbers[j-1] > index))


/* from the string sort */
    while ((j > 0) && (strcmp(StuData[j-1].name,index[j].name)==1))

Where the heck did (strcmp(...,...) == 1) come from?

If you want to test to see whether one string is greater than the other, wouldn't it be more like (strcmp(...,...) > 0)

Regards,

Dave
  #7  
Old 08-Feb-2008, 17:49
odin607 odin607 is offline
New Member
 
Join Date: Feb 2008
Posts: 5
odin607 is on a distinguished road

Re: Sorting (structures and arrays fun fun..)


well if the string on the left its larger, and returns 1 as far as i know.... and if the right is bigger it returns -1.. and 0 if theyre the same.. therefore >0 would be the same as == 1


i tried >0 after seeing yer response and got the same results

but lol that doesnt really matter...at this point im just throwing around different stuff till someone can be like.. "try this" =p

and "heres where it stands now" is a saying... like "this is what i have now" >_>;
  #8  
Old 08-Feb-2008, 17:58
odin607 odin607 is offline
New Member
 
Join Date: Feb 2008
Posts: 5
odin607 is on a distinguished road

Re: Sorting (structures and arrays fun fun..)


got it to work finally yay lol (spent more time on the lame sort the prof gave us than the program itself i bet lol.)
CPP / C++ / C Code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {                // = Main Structure =
        char name[26];                //               *
        int midterm[3];                //                   *
        int final;}                        //                   *
STUDENT;                                // ==================

void Process(STUDENT StuData[]);                                        // Processing Function (fills in the
main structure with data from input file)
void Sort(STUDENT numbers[], int array_size);                // Sorting Function (sorts
the main structure data alphabetically)
void Print(STUDENT StuData[], int array_size);                // Printing
Functionv(sorts name part of the main structure withing array StuData)

STUDENT StuData[6];                                // Array containing main structure
char last[13], first[13];                // Arrays to hold names pre-concatenation

void main()
{
        STUDENT StuData[6];                        // Array containing main structure

        printf("Executing processing function . . .");
        Process(StuData);
        printf("Executing sorting function . . .");
        Sort(StuData,6);
        printf("Executing printing function . . .");
        Print(StuData,6);
}

void Process(STUDENT StuData[])
{
        int i;                                                // Counting variable
        FILE *Fi=NULL;                                // File Input

        printf("  Started.\n");
        printf("Opening input file . . .");

        Fi=fopen("student_data.txt","r+t");

        if(Fi == NULL)                                                                        // =File opening status=
        {                                                                                                //                        *
                printf("             Failed!\n");                        //                        *
                exit(1);                                                                        //                        *
        }                                                                                                //                        *
        if(Fi != NULL)                                                                        //                        *
                printf("             Successful!\n");                // =====================

        printf("Processing starting . . .");

        for(i=0;i<6;i++)
        {
                fscanf(Fi,"%s%s%d%d%d%d",last,first,&StuData[i].midterm[0],&StuData[i].midterm[1],&StuData[i].midterm[2],&StuData[i].final);
                strcpy(StuData[i].name,last);
                strcat(StuData[i].name," ");
                strcat(StuData[i].name,first);
        }
        printf("            Finished!\n");
}

void Sort(STUDENT StuData[], int array_size)         // Pre-made Function (edited)
{
        int i, j;
        char index[26];

        for (i=0;i<array_size;i++)
        {
                strcpy(index,StuData[i].name);
                j = i;
                while ((j > 0) && (strcmp(StuData[j-1].name,index)>=0))
                {
                        strcpy(StuData[j].name,StuData[j-1].name);
                        j = j - 1;
                }
                strcpy(StuData[j].name,index);
        }
        printf("     Finished!\n");
}

void Print(STUDENT StuData[], int array_size)
{
        int j;                // Counting variable

        printf("    Started!\n\n");

        for(j=0;j<6;j++)
        {
                printf("%s: %d %d %d
%d\n",StuData[j].name,StuData[j].midterm[0],StuData[j].midterm[1],StuData[j].midterm[2],StuData[j].final);
                if(j==(array_size-1))
                        printf("\n\n\nPrinting . . .                       Finished!\n");
        }
        printf("Input file anaylsis . . .            Complete.\n\n");
}

so basically i needed my sort function to look like that =/

thanks for the trouble, reminding me to try the str commands again was clutch
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is an array? Howard_L C Programming Language 3 05-Oct-2007 05:11
How Do I Create a Function that returns a usable array of pointers to structures? UncleRic C Programming Language 2 03-Sep-2007 11:17
arrays of structures as an argument? 25cents C++ Forum 6 06-Dec-2006 15:20

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:53.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.