[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Filling an array from a file of strings and search recursively within the array

Posted on 2004-11-15
1
Medium Priority
?
241 Views
Last Modified: 2010-04-15
I need to read an unknown number of strings out of a file (names) storing them in an array of size 20.  The file may have les than or more than 20 names in the file.  The array is then sorted in ascending ASCII order using a selection sort, printed and search for the position of a name until the user enters "quit".  I have written the program with two exceptions:

1.  Building the array from the file using fscanf.
2.  Ending the binary search by entering quit.  The user is continusously prompted...I cannot get the function to quit.

Here is the program:

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

#define NUMWORDS 20
#define LENWORDS 25

//Prototypes
void displayName (void);
void selection (char words [][]);
void printWords (char words [][]);
void getInput (char words [][]);
int search (int start, int end, char words [][], const char s[]);

//Main controls the

int main (int argc, char *argv[])
{
   FILE *infptr;
   char words [NUMWORDS][LENWORDS];
//{"love","mary","had","a","little","lamb","its","fleece","was","white","as","snow","ittzy","bittzy","spider","came","down","the","water","spout"};
   
   if (argc <= 1)
   {
     printf("\nNot enough command line arguments.\n");
   }
   infptr = fopen (argv [1], "r");
   if ((infptr)==NULL)
   {
      printf("\nERROR: Unable to open data for reading.\n\n");
   }
   if ((argc>=2) && ((infptr)!=NULL))
   {
      int i;
      for (i=0; i < NUMWORDS; i++)
        words [i] = fscanf (infptr, "%s", words[]);
      displayName ();
      selection (words);
      printWords (words);
      getInput (words);
   }
   //Format and finish.
   printf ("\n");
   return 0;
}

//The name of the programmer is displayed.
void displayName (void)
{
   printf("\nName: Chandra Parr\n\n");
}

void selection (char words [][LENWORDS])
{
   int i,j,k;
   int currMin=0;
   char temp[NUMWORDS];

   for (j=0; j < 19; j++)
   {
      currMin=j;
      for (k=j+1; k < 20; k++)
         if (strcmp(words[currMin], words[k])>0)
            currMin=k;
         strcpy(temp, words[currMin]);
         strcpy(words[currMin], words [j]);
         strcpy(words[j],temp);
   }
}

void printWords (char words [][LENWORDS])
{
   int i;

   for (i=0; i < NUMWORDS; i++)
      printf ("\n%d: %s", i, words[i]);
   printf ("\n\n");
}

void getInput (char words [][LENWORDS])
{
   int result;
   int scanResult;
   char searchWord [LENWORDS];

      do
      {
         printf("Please enter a word to search (or quit to stop): ");
         scanResult = scanf ("%s", searchWord);
         if (scanResult == 1)
         {
            result = search (0, NUMWORDS - 1, words, searchWord);
            printf("The word \"%s\"", searchWord);
            if (result == -1)
               printf (" was not found.\n");
            else
               printf (" was found at position %d.\n", result);
         }
         }
         while (scanResult == 1);
}

int search (int start, int end, char words [][LENWORDS], const char s[])
{
   int middle, cmpRes;
   
   if (start > end)
      return (-1);
   else if (start == end)
      if (strcmp(words[start], s) == 0)
         return start;
      else
         return (-1);
   else
   {
      middle = (start + end) / 2;
      cmpRes = strcmp(words[middle],s);
      if (cmpRes == 0)
         return middle;
      else if (cmpRes > 0)
         return search(start, middle - 1, words, s);
      else
         return search (middle + 1, end, words, s);
   }
}
0
Comment
Question by:csunnypl2
1 Comment
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 500 total points
ID: 12591513
>>1.  Building the array from the file using fscanf.
Instead of:

     for (i=0; i < NUMWORDS; i++)
        words [i] = fscanf (infptr, "%s", words[]);

try:

     for (i=0; i < NUMWORDS; i++)
        if ( fscanf (infptr, "%s", words[i]) == NULL ) strcpy(words[i],"");

>>2.  Ending the binary search by entering quit.  The user is continusously prompted...I cannot get the function to quit.
Instead of:
         while (scanResult == 1);

try:

         while (scanResult == 1 && strcmp(searchWord,"quit") != 0);

Paul
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

873 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