2d Array searching etc

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 577 Views - Last Post: 24 January 2013 - 06:58 AM Rate Topic: -----

#16 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: 2d Array searching etc

Posted 23 January 2013 - 09:45 PM

Quote

Anyone got any idea on how i can implement a search function into this code?

As jim said, you are not really adding anything to a "data base"; rather, you are adding data into temporary arrays that only exists for the life of your add function.
Was This Post Helpful? 0
  • +
  • -

#17 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: 2d Array searching etc

Posted 24 January 2013 - 12:37 AM

View Postafortunado, on 23 January 2013 - 04:31 PM, said:

So your saying make a class which will include all the function names, slightly confused could you maybe demonstrate with code? thanks


This is the simplest program snippet I have that shows it. It's for a video database (just a flat file, not a relational database).

Above main() is the prototype of the struct - so for every record (every struct), the title and the rating for it, will always be together in the same record - even though the title is obviously a string, and the rating is an integer.

Note that in this simple example, the program is very limited - the finished program is substantially longer and has a LOT of lines of code that are only related to the exact display that I wanted to see.

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


#define SIZE 5

struct video {         //prototype for the struct (or record)
   char title[80];
   int rating;
};

void menu(struct video videos[SIZE]);
void sortIt(struct video videos[SIZE], int keyNum);
void printIt(struct video videos[SIZE]);

int main(void) {
   struct video videos[SIZE];    //only now is the array of records created
   char intake[150];
   FILE *fp;
   int i,j;
   if((fp=fopen("videoDB.txt", "rt"))==NULL) {
      printf("Error! File Not Found\n");
      return 1;
   }
   i=0;
   while((fgets(intake, 149, fp))!= NULL) {
      j=strlen(intake);
      while(intake[j] != ' ') 
         --j;
      
      intake[j++]='\0';
      //printf("%s*\n",intake); getchar();
      strcpy(videos[i].title, intake);
      sscanf(intake+j,"%d ", &videos[i].rating);
      printf("%s %d\n",videos[i].title, videos[i].rating); 
      
      ++i;
   }
   fclose(fp);
 
   menu(videos);

   printf("\n");
   return 0;
}
void menu(struct video videos[SIZE]) {
   int choice;
   do {
      printf("\n   1. Display All Videos (n/a)          2. Search For a Video (n/a)\n"\
             "\n   3. Sort Videos By Title              4. Quit\n");

   
      printf("\nEnter your selection: ");
      scanf("%d", &choice);
      printf("\n");
      switch(choice) {
         case 1: printIt(videos);
                 break;
         case 2: printf("Choice not available yet\n");
                 break;
         case 3: sortIt(videos, choice);
                 printIt(videos);
                 break;
         case 4: printf("Goodbye\n\n");
                 break;
         default: printf("try again, please\n");
      }
   }while(choice != 4);
}

/* later on, keyNum could be used to direct the sort to the proper struct member for that sort */
void sortIt(struct video videos[SIZE], int keyNum){  //this is insertion sort
   int i,j;
   struct video temp;
   for(i=1;i<SIZE;i++) {
      j=i;
      while(j>0 && strcmp(videos[j].title, videos[j-1].title)< 0) {
         temp = videos[j];
         videos[j] = videos[j-1];
         videos[j-1] = temp;
         --j;
      }
   }
}

void printIt(struct video videos[SIZE]) {
   int i;
   printf("\n        Title                   Rating\n"\
          "================================================\n");
   for(i=0;i<SIZE;i++) 
      printf("%3d) %-28s %2d\n",i+1,videos[i].title,videos[i].rating);
}



Don't take the above as good code - it's not. It's just for idea's. Obviously, it's not a finished program, and I have no idea if it even runs - but the ideas are good.

Your program should have similar functions, doing similar things: sorting, searching, displaying, writing out to the hard drive, getting the data from the hard drive into the array, editing, etc.

The key things are:

*start with the first function you need. Code it, and compile as you go. Fix all errors and warnings (unless the warning is trivial). Test that ONE function a few times to see if it's working OK, before going on to the next function.

You can't test anything, if you can't compile and run the program. Keep it able to run.

*build from one function, to the next, compiling and testing, as you go. Make each function do something, that the other functions don't do. That makes debugging much easier. Limit or completely avoid, using global variables, and goto's.

*use variable names that indicate their purpose. We understand that i and j are loop iteration variables, but other than that, stick to short names, that have meaning for the program.

*put off the details until later. Get the basic "flow" (the logic) of your programs functions working first, then add your display etc. details, later.
Was This Post Helpful? 0
  • +
  • -

#18 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,457
  • Joined: 25-December 09

Re: 2d Array searching etc

Posted 24 January 2013 - 06:58 AM

Quote

*start with the first function you need. Code it, and compile as you go. Fix all errors and warnings (unless the warning is trivial). Test that ONE function a few times to see if it's working OK, before going on to the next function.

Until you have much more experiance in C/C++ every warning should be treated as an error and fixed. Your program should compile without errors and warnings. You should also insure that your compiler is generating the maximum number of warnings.

Compile often, many times after adding just one line of text. The sooner you compile your code the fewer errors you will have to find and correct.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2