URGENT (500pts): Arrays - Some data not displaying correctly

Posted on 2004-09-22
Last Modified: 2010-04-15
Theres quite a few things I really need fixing up but the brainstorming bit is too hectic now. Anyway the full project source code is as follows:

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

FILE *ifp;
char tmpstudentanswer, tmpactualanswer;
char fullnameArray[30][19], stunumArray[30][6];
int fullnamechars = 0, stunumchars = 0, stuanswerchars = 0;
int recordno = 0, highscorestudentno, lowscorestudentno;
int numcorrect, highscore = 0, lowscore = 70;
float averagescore = 0;
char answer[69];
char studentanswer[30][70];

void sort() {

      char temp[20], tempstunum[7], tempanswer[70];
      int swaps, i;
      swaps = 1;

      while(swaps > 0) {

            swaps = 0;
            for(i=0; i<25; i++) {

                  if(strcmp(fullnameArray[i],fullnameArray[i+1]) > 0) {

                        /* sort name */
                        strcpy(temp, fullnameArray[i]);
                        strcpy(fullnameArray[i], fullnameArray[i+1]);
                        strcpy(fullnameArray[i+1], temp);

                        /* sort student number */
                        //strcpy(tempstunum, stunumArray[i]);
                        //strcpy(stunumArray[i], stunumArray[i+1]);
                        //strcpy(stunumArray[i+1], tempstunum);

                        /* sort answer */
                        //strcpy(tempanswer, studentanswer[i]);
                        //strcpy(studentanswer[i], studentanswer[i+1]);
                        //strcpy(studentanswer[i+1], tempanswer);




void skipeol() {

      char c;

      c = fgetc(ifp);
      while (isspace(c) && !feof(ifp)) {
            c = fgetc(ifp);
      if (feof(ifp)) {
      ungetc(c, ifp);

void displaystats() {
      printf("Highest score: "); printf("%i achieved by ", highscore);

      for (fullnamechars = 0;fullnamechars<19;fullnamechars++){
            printf("%c", fullnameArray[highscorestudentno][fullnamechars]); //print full name

      printf("Lowest  score: "); printf("%i achieved by ", lowscore);

      for (fullnamechars = 0;fullnamechars<19;fullnamechars++){
            printf("%c", fullnameArray[lowscorestudentno][fullnamechars]); //print full name


      //calculate average here
      averagescore = averagescore / 25;

      printf("Average score: "); printf("%.2f", averagescore);


void displaystudents() {



            if (recordno > 0) { //dno't print the first line

                  //sort student names in alphabetical order of surname

                  /* print student details */
      //            printf("\n*************************\n\n");
      //            printf("\n");

                  //print full name of student
      //            printf("Full name: ");

                  //print student id'

      //            printf("Student ID: ");

                  for (stunumchars = 0;stunumchars<7;stunumchars++){
                        printf("%c", stunumArray[recordno][stunumchars]);
                        //c = fgetc(ifp);


                  for (fullnamechars = 0;fullnamechars<19;fullnamechars++){

                        //may need a sort() function before displaying names
                        printf("%c", fullnameArray[recordno][fullnamechars]); //print full name

      //            printf("\n"); //next line

      //            printf("\n"); //next line

      //            printf("Answers right: ");
                  //make sure numcorrect variable starts off with 0
                  numcorrect = 0;

                  for (stuanswerchars = 0;stuanswerchars<70;stuanswerchars++) {

                        tmpstudentanswer = studentanswer[recordno][stuanswerchars];
                        tmpactualanswer = answer[stuanswerchars];

                        if(tmpstudentanswer == tmpactualanswer ){

                              numcorrect = numcorrect + 1;
                              //add one to variable (increment variable)

                        else {

                              //dont increment variable by one (do nothing)


                        //printf("%c", studentanswer[recordno][stuanswerchars]);

                  //calculate statistics such as low, high, and average scores
                  if(numcorrect > highscore) { highscore = numcorrect; highscorestudentno = recordno; } //check if score exceeds highest score
                  if(numcorrect < lowscore) { lowscore = numcorrect; lowscorestudentno = recordno; } //check if score is below lowest score
                  averagescore = averagescore + numcorrect; //calculate the average score

                  //print score for this student
                  printf("%i", numcorrect);




void main() {

      char c;
      int i = 0, answerno = 0;
      int namecount;

      ifp = fopen("results.txt", "r");
      if (ifp == NULL) {
            printf("Can't open file. NB: Make sure file (results.txt) is in the directory. \n\n ");

      while (recordno != 26) { //loop until all 26 records are processed

            while (i != 155) {

                  //get character by character
                  c = fgetc(ifp);

                  if (recordno == 0) { //get 70 answers only, nothing else

                        //if first record of file, then get the 70 answers
                        //and put them in an array
                        if (i == 85) { //start fro mthe 85th character (used to be >= not ==
                              //printf("%c", c);
                              for (answerno = 0;answerno<70;answerno++) {

                                    answer[answerno] = c;
                                    c = fgetc(ifp);
                                    //printf("%c", answer[answerno]); //just displays correct answers


                  else { //record is 2nd and beyond

                        if (i == 40) { //grab student full name

                              for (fullnamechars = 0;fullnamechars<18;fullnamechars++){
                                    fullnameArray[recordno][fullnamechars] = c;
                                    c = fgetc(ifp);



                        if (i == 69) { //grab student id'

                              for (stunumchars = 0;stunumchars<7;stunumchars++){
                                    stunumArray[recordno][stunumchars] = c;
                                    c = fgetc(ifp);



                        if (i == 85) { //start fro mthe 85th character to get student answers

                              for (stuanswerchars = 0;stuanswerchars<71;stuanswerchars++) {
                                    studentanswer[recordno][stuanswerchars] = c;
                                    c = fgetc(ifp);



            //skip the white spaces then move to next line

            //printf("Student # | Student Name | Score ");

            //display student record

            //reset i and increment recordno



      //display statistics



Of course, I use a text file that has the following content in results.txt:

703000001001061803001   4521 #0001 Y  S                     ---                      3224141331223242353214431413333241142124131344421241422341323434232331                                                                                                                                      
703000002001061803001   4521 #0001 Y  S ARNOLD TOM          M--0117811110234   211   1223244331221214543214232332334244144331144241121241422343331434132313                                                                                                                                      
703000003001061803001   4521 #0001 Y  S ZELWEIG BETTY       ---      1110235         3223144332221243153234432213311214142114442114421244422321342434231341                                                                                                                                      
703000004001061803001   4521 #0001 Y  S SIMPSON BART        ---02*2821110236         1424141321221224123114132142243241132334144314431243422211321434232341                                                                                                                                      
703000005001061803001   4521 #0001 Y  S JACKSON REGINALD    ---      1110237         2324144332211144453434432112234224212311134341121344432341331334331231                                                                                                                                      
703000006001061803001   4521 #0001 Y  S TREWILLIGAR B       M--0827781110238         4223434331221324121325432413233211132144244324121244421333344414222331                                                                                                                                      
703000007001061803001   4521 #0001 Y  S MOORE SUSAN         M--1116781110239         3223434331221244151244232413342241132144132324321244423141321434231331                                                                                                                                      
703000008001061803001   4521 #0001 Y  S MARVIN LEE          ---0707811110240         2323141333241421153244432432343234112121143341221244441341314434132331                                                                                                                                      
703000009001061803001   4521 #0001 Y  S SPEARS BRITTNEY     M--      2220345         2324314321411212524412242142333244144324144244124341424324321411233421                                                                                                                                      
703000010001061803001   4521 #0001 Y  S SMASHMOUTH PETER    F--0327802220346   203   1423444331121321454212232413344244142142142444421244422341321134232231                                                                                                                                      
703000011001061803001   4521 #0001 Y  S SIMMONS RED         M--0210812220347         2324143323211244234212132411342214114141131142421244422321331414232331                                                                                                                                      
703000012001061803001   4521 #0001 Y  S LAKE TIFFINAY       F--      2220348         1325144324313243152224432333341214142144322241431241444321342434232341                                                                                                                                      
703000013001061803001   4521 #0001 Y  S YAMAMOTO LAIN       ---      2220349         2415143323221224353214431413314134142141133344431244443341123434132213                                                                                                                                      
703000014001061803001   4521 #0001 Y  S SOMMERS DONNA       ---      2220350         1224244331121344323224232233222234142131334421241244422321332434232121                                                                                                                                      
703000015001061803001   4521 #0001 Y  S LOYD KEN            M--1215822220351         3323444333221444154214232413344214132124124344221244422331313334221331                                                                                                                                      
703000016001061803001   4521 #0001 Y  S KINNISON KIM        F--1229802220356         2423422333411244454112132221342443132341132131121244434231311434132331                                                                                                                                      
703000017001061803001   4521 #0001 Y  S WOOLDRIDGE HELEN    M--1210692220357         3222131321223334353214452433144241332141133134241244421321221424434311                                                                                                                                      
703000018001061803001   4521 #0001 Y  S RAMOS PAULO         ---0308732220358         1323144331221224353214531433334241142144132341424243422341312434222411                                                                                                                                      
703000019001061803001   4521 #0001 Y  S JACKSON LINDA       F--0705832220359         3223144332241323454212432233244244212144132434121241422341321334232441                                                                                                                                      
703000020001061803001   4521 #0001 Y  S NGUYEN TOM          ---      2220360         1424141331123244353124232413341134432141141134131241422341221434232211                                                                                                                                      
703000021001061803001   4521 #0001 Y  S DALGLEESH PETER     M--1005822220361         1223244331221424454222431413331244142114342311421244442341311414232333                                                                                                                                      
703000022001061803001   4521 #0001 Y  S HAIM COREY          M--0922812220362         3225144333221243542224232413313214132144132344121244423341311434132331                                                                                                                                      
703000023001061803001   4521 #0001 Y  S DELENTIS CHRISTINE  M030316032220363         1223244333341244453314432113312134134114242314121241222341314134132321                                                                                                                                      
703000024001061803001   4521 #0001 Y  S HANNAH DARYL        M--0118832220364         3223141331221234532234232413344241212144334124421241422323313434422131                                                                                                                                      
703000025001061803001   4521 #0001 Y  S DOUGLASS MICHAEL    ---0711822220365         1223441323241224152212232413343241332141133143221244422321221214332241                                                                                                                                      
703000026001061803001   4521 #0001 Y  S STOCKDALE A         ---      1110366   185   3313144334221244541314432133244144112341122214221244442341341434232321                                                                                                                                      


Just copy all these funny looking data and paste into notepad and save as results.txt

Then put the program and file together int he same directory and compile+run.

The numbers are correct but they are not linked correctly to each of those people because i used a sort(); function which re-arranges all the names in alphabetical order. If I don't use the sort function i believe the numbers would link to each other nicely.

Heres another problem..

the left side that shows the 7 digit numbers are supposed to be ID numbers of these people. But as you can see, and according to results.txt, its not correct.

This is VERY difficult.

Please help *URGENTly*.

Thanks very much.
Question by:seelow
  • 4
LVL 45

Accepted Solution

sunnycoder earned 250 total points
ID: 12130607
Hi seelow,

>char fullnameArray[30][19], stunumArray[30][6];
combine them together into a struct

struct student_info {
        char fullname[19];
        char num[6];

struct student_info[30];

Now sort by student_info[i].fullname and swap the whole structs instead of just names ... That way you will not have to worry about moving numbers along with students.

If you have too little time left, then scan the code. Look for every instance where you swap names for sorting and at the same place, swap the numbers at the same indices too. Less elegant, but will save your neck


Author Comment

ID: 12130748
Yeah I realised its too late. I knew structs would be handy. But I know too little about it to even attempt it. So I went with arrays. This project has driven me crazy :)
LVL 16

Assisted Solution

PaulCaswell earned 250 total points
ID: 12131027
Dunno if I'm too late but heres an option:

Use an index.

Make a new array of integers:

int index[30];

Start off by having it in linear order.

for ( i = 0; i < 30; i++ ) index[i] = i;

Then, to access ANY of your array elements, use the index first:

I.E whenever you see something like:




All you then need to do is sort the index, not the arrays.

              if(strcmp(fullnameArray[index[i]],fullnameArray[index[i+1]]) > 0) {

                    /* Swap items */
                    temp = index[i];
                    index[i] = index[i+1];
                    index[i+1] = temp;



Instead of swapping the strings around, swap the index. Its like one of those old telephone switchboards with the plugs and wires. If you want to ring your mom the phone company dont send a bike out to the phone you are ringing and get it moved to your moms house, they just connect the phone you are on with the one at your moms house.

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.


Author Comment

ID: 12910708
Unfortunately I couldn't resolve my problem through here due to the time I had left for this work during the time. So I will need a refund.

Author Comment

ID: 12991533
"No comment has been added to this question in more than 21 days, so it is now classified as abandoned."

I did post a comment above:


Comment from seelow
Date: 12/27/2004 09:34PM PST
 Your Comment  

Unfortunately I couldn't resolve my problem through here due to the time I had left for this work during the time. So I will need a refund.


My problem WASN'T solved. I can't afford to lose 500 points when a problem wasn't solved. If you insist on splitting the points I would like half that amount of points refunded please. 500 is alot to lose.

Thank You.

Author Comment

ID: 12991539
Just to make my post visibly clear, I will post my comment again in hopes that my comment is seen by ANY adminstrators or cleanup volunteers.

I WISH to have a refund. If you feel they should be rewarded points then do split 125 to each of them then. I would like 250 refunded back to me. I will already run on a very unfair budget by giving away 200+ for a problem that was not solved.

Thank you.

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question