Adak 419 Nearly a Posting Virtuoso

Turbo C has it's quirks, but here's some hints I use (and I use Turbo C/C++ (the C compiler portion), very often.

1) Use Turbo C/C++, NOT "Turbo C". Turbo C was an earlier product, and is buggy. Turbo C/C++ is fine - I recommend ver. 2.0, but 1.01 is also good.

2) Make the default properties of TC's window, just big enough for a full screen console (not graphic), window. If you set it up with a larger buffer, or a smaller buffer, you'll have problems, because it runs in a small amount of memory

You CAN get a full window, and a nice screen look, with no "black" screen, when you exit.

This is a legacy product and it's free - and very useful for small programs. I use it all the time (almost daily). Very easy interface, and help is quick, just one key combo.

Meaco, what kind of a pointer program are you trying to make? What is the part that is difficult?

If you have some code, please post it, and describe in detail about the program.

Ancient Dragon commented: Every version of Turbo C or Turbo C++ is terrible. -5
Adak 419 Nearly a Posting Virtuoso

On the right hand side of the forum, there is a green box labeled "Related Forum Features", which includes a C tutorial. Google has several others.

Most C forums that are active, have either a tutorial, a FAQ, or both.

You can't do what you want with (almost) no knowledge of C. It's like trying to lose weight - first you have to restrict your calories enough, and THEN you start to lose weight. It doesn't work the other way around, no matter who you are, or how much you might pray for it to be like that. ;)

We had to work to learn C, and you will also - or you won't learn C. Asking for help finding a tutorial, when you have a link right in front of you, is somewhat disingenuous, imo.

Adak 419 Nearly a Posting Virtuoso

I don't know about helping you "get" code, but I can help you *write* code for this histogram.

What do you have for input, (how is it organized, especially)?

Post up your code that you have so far - you do need to show some work, here - and make a small input file example and attach it to your post (or just wrap it in code tags, also and post it as well).

You should read up on just what a histogram is, if you don't already know. (I think you do know).

And tell us what you're stumped on, please. "Help" is not all that helpful, oddly. "Help with *SOMETHING*, is much more -- yeah! Helpful!! :)

Adak 419 Nearly a Posting Virtuoso

This program shows how to do a simple selection sort (almost the same as a bubble sort), keying on one column of a 2D array.

/* shows how to a 2D array, using one column (the last column) as the key.
*/

#include <stdio.h>

void printArray(const int a[3][4]);

int main()

{
  int pass;
  int i;
  int j, k;
  int hold;
  int array1[3][4]= {5,34,78,112,1,56,86,142,3,45,29,74};

  printf("Employee number   1st Quarter Sales   2nd Quarters Sales   Total Sales\n");

  printArray(array1);
  
  for (i = 0; i < 3; i++) {
    for(j = i+1; j < 3 ; j++) {
      if(array1[i][3] < array1[j][3]) {  //swap all cols in the row
        hold= array1[i][3];                  //this is our key column
        array1[i][3]=array1[j][3];
        array1[j][3] = hold;

        hold = array1[i][2];
        array1[i][2] = array1[j][2];
        array1[j][2] = hold;

        hold = array1[i][1];
        array1[i][1] = array1[j][1];
        array1[j][1] = hold;

        hold = array1[i][0];
        array1[i][0] = array1[j][0];
        array1[j][0] = hold;

      }
    }
  }
  printArray(array1);

     // system("PAUSE");

  printf("\n\n\t\t\t     press enter when ready");
  getchar();
  return 0;
}

void printArray(const int a[3][4])

{
  int i;
  int j;
  printf("\n\n");
  for (i =0; i < 3; i++) {
    for (j=0; j < 4; j++) {
      printf("%9d  ", a[i][j]);
    }
    printf("\n");
  }
}

This is not the best way to do this, however. (Imagine doing this with a 2D array with 100 columns <eek!>.) The best way is to NOT move any of the data, and swap values in an index (or pointer) auxiliary array, instead. That allows the data to be printed out in sorted order, but no data will have to be moved.

aslk commented: Thank you for your time. +1
Adak 419 Nearly a Posting Virtuoso

Why NULL out any char's?

Your buffer[index] is currently OUTSIDE a parenthesis or double quote, or And you program should start as OUTSIDE.

Your buffer[index] is currently INSIDE a parenthesis or double quote. Both cases have to be dealt with. When buffer[index] goes INSIDE, then match equal's ')' or '"' char. When buffer index meets that char next, then it's status will switch to OUTSIDE again.

if(your status is OUTSIDE), and your buffer[index] == ';', then increment your line count.

In a program like this, less is more. Don't have your program do things that obfuscate your goals.

Adak 419 Nearly a Posting Virtuoso

Thank you for that but if i wanted to search the file for other records can this code be modified to achieve that

Can it be modified to search through records? Yes, but you wouldn't want to do that.

In a program, the *design* has to fit the purpose (it's a form and function get-together).

For what you want, the way to go is to make each record into a struct, which allows all these fields for one person, to be "glued" together, like an object, with parts: name, registration number, date of birth, sex, age, address, etc.

Those are handled in a different (binary), file mode, and you wouldn't need to go "hunting" for the registration number, because the struct would have that information, already known by the program.

The field for "registration number:" would never contain the string "registration" or "number" or ":". It would just contain the actual number, and the structure would show the program just where to find it, within the record.

It's very efficient that way - fast and a smaller amount of data per record.

You'll need to do some studying between now, and when you can work with structs - maybe 30-90 days?

You'll need a good book, and time to study it. Practice is also very important. It's all fine to study how to swim on dry land, but you LEARN how to swim, by getting into the water. And if you don't practice …

Adak 419 Nearly a Posting Virtuoso

Fscanf() has it's place, but the whole family of scanf() is very "fragile" and leads to code that breaks easily.

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

int main() {
  int i, j, regnum; 
  FILE * fp;
  char str[50] = { '\0' };
  char number[10] = { '\0' };

  /* if the file is in another directory, you need to include
  the full path and filename: C:\\directory\filename with two 
  backslashes on the first directory, or C:/directory/filename
  using just one forward slash.

  Weird eh? ;)
  */
  fp = fopen("C:/Rtest.txt", "rt");
  if(fp==NULL) {
    printf("\nFile Error - terminating the program");
    return 1;
  }

  fgets(str, sizeof(str), fp); 
  printf("\n\n%s\n\n\nYour number is: ", str);
  for(i = 0, j = 0; i < strlen(str); i++) {
    if((str[i] >= '0') && (str[i] <= '9')) {
      putchar(str[i]);
      number[j++] = str[i];
    }
  }
  number[j] = '\0';
  regnum = atoi(number);  

  printf("\nRegistration Number is: %d", regnum);

  fclose(fp);       
  printf("\n\n\t\t\t     press enter when ready");

  i = getchar();
  return 0;
}

Edit: Added some code to save the digits in a separate integer, regnum.

WaltP commented: We don't do other's homework for them. We help them do their own. -2
Aia commented: To balance the negative of Mr WaltP, just because you added a couple lines more. +8
jephthah commented: i am not a fan of giving away code wholesale. And while scanf() is usually a poor choice for input, fscanf and sscanf are powerful functions and are very good choices when the input is constrained. -1
Adak 419 Nearly a Posting Virtuoso

Suggestions:

1) "Beginning C Programming" by Ivor Horton - fine book!

2) Many on-line tutorials, like the one on this very site (right hand side)-->

3) Keep reading a couple good and busy, C forums. You'll pick up a lot, and keep from forgetting what you've already learned.

4) Find little puzzles and problems on the forums, and try to make a small program to solve the posters question or problem, if it seems about at your level.

And welcome! :)

Adak 419 Nearly a Posting Virtuoso

On this forum, we try to NOT give out code, until after the poster (you), have shown some work to make the program.

We want to HELP, not become "homework central" for every student who would like to have their assignment done for them. Do you see what I mean?

Actually, I already wrote up a little program just for you, but I'm not going to post it unless and until you show some EFFORT.

So get posting! ;)

Adak 419 Nearly a Posting Virtuoso

Empirically, you have proven that it is indeed, too large. Remember, not only must that quantity of RAM be present, but it also must be contiguous. Also, you are asking for memory only from the stack.

Try re-booting your system (to enable the largest RAM possible), and if that should fail, you'll want to use malloc() or calloc() for your memory request. It's more complicated, but it gets memory from a larger resource than the stack can offer.

Adak 419 Nearly a Posting Virtuoso

Aurorian, here's an important idea to remember about floats and doubles: they can't perfectly represent every number.

Integers can represent every (whole) number, but non-integer data types can't. They'll be *very* close, but not quite perfect. So making comparisons of float1 == float2 type, are a bad idea, overall.

You want to use integers for comparisons like that.

Adak 419 Nearly a Posting Virtuoso

Nothing Worked :(

Oh, it will work - but you have the file you're trying to open, in the wrong directory, or an IDE set up problem.

Look into your help files on Debug and Release versions, and find out what directory your IDE is really running your program, from. You have project settings (default probably), that should be able to show you.

Adak 419 Nearly a Posting Virtuoso
#
printf("\nStudent [%d]\n",i+1);
printf("Student name: ");
scanf("%s",Stud[i].name);

printf("Course: ");
scanf("%s",[B]&[/B]Stud[i].course);

/* Course is a char array, so the name of the array is the address of the base of the array. Delete the '&'.
*/

I did not review the rest of your code. If it still have problems you can't fix, post back.

Adak 419 Nearly a Posting Virtuoso

Oscillator, *now* tell us how you really feel about this thread, won't you?

;) ;)

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Shubh. :)

A few important suggestions for you:

1) For your problem, start a new thread. Using someone else's is called "thread hijacking", and will get people upset with you.

More people will see your thread when you start a new one, and respond.

2) When you post code, ALWAYS highlight it and click on the # icon in the advanced editor, or "Code" word in the quick reply window. That makes your code keep good formatting on the forum. Hopefully, you use good formatting in your code, to help spot bugs and show the relationship between lines of code that are primary, and those that are dependent (indented 2 - 5 spaces).

Salem commented: Barely contained restraint here ;) +19
Adak 419 Nearly a Posting Virtuoso

It wants constant void pointers, so you have to cast from what I've seen. .

I have 4 versions of Quicksort, and none of them need casting like the qsort() version.

I'd like to see some qsort examples that eliminate casting to and from the call to qsort, and it's compare function.

When you want to sort strings, the first thing you do is cast the string array to type void *:

qsort((void *) strArray, . . .

Then the compare function has to cast the sub array range, to constant void *, for both low and hi, as well.

Adak 419 Nearly a Posting Virtuoso

Be kind, Aia - she's only been programming for a few days.

Adak 419 Nearly a Posting Virtuoso

What we require, is that you take a stab at doing this assignment, and post up your code. Tell us where you're stuck.

We can help, and the fact that it's Turbo C will not be a problem. I use an early version of it, myself. (The C side of Turbo C/C++, actually).

There are all kinds of examples and tutorials out there, and you should have class notes and books for reference, as well.

So get to it, and post back when you have something specific to ask about.

"I need help ", by itself, just means "I'm too busy to be bothered". In which case, we are too busy, as well.

Adak 419 Nearly a Posting Virtuoso

I know you might not want to accept this, but there ARE limits to what your console window can show.

What about having your program pause displaying more lines after X number have been printed? Then, when you hit <enter> key, the next page of lines would be printed up.

I use that, a lot, and it works well. The mod operator is key:

if(++lines % 20 (whatever number you like) == 0) {
  getchar();
}

Put that right inside your busy loop, and edit out the "whatever number you like" bit, and try that.

Every line of output has a newline either at the beginning or the end of the print statement, right?

Adak 419 Nearly a Posting Virtuoso

You need to assign the address that fopen() returns to your FILE * (pointer).

FILE *fp;

fp = fopen("Keywords.txt", "rt");

if(fp == NULL) {
   printf("\nError opening file\n");
   return 0;
}
Adak 419 Nearly a Posting Virtuoso

The getchar()'s are there to pull newlines off the keyboard buffer. In this case, since the program is scanf()'ing for numbers, the getchar()'s are not needed.

Scanf() will jump over a newline, if it is looking for a number.

Money should not be an integer, when the user enters it. It should be a float or double. Then, you can multiply money times 100 to deal with it as an integer, as all pennies. Sounds odd, but it works *really* well.

If percentage is a float, it should not be scanf()'d in with a %d - you know that's haywire! Same mistake with year variable. :(

You know that "percent means hundredths" (8.5% == 0.085), but I'm not sure all your user's know that. You might want to take in the percent as just an 8.5 kind of a float, and then divide it by 100, using your program, instead of relying on the user's math skills.

Make those changes, and see how it works.

Adak 419 Nearly a Posting Virtuoso

Thanks, Salem.

Adak 419 Nearly a Posting Virtuoso

The newptr should be sized for a pointer, so add an asterisk to this line:

newPtr = (struct Stack *) malloc(sizeof (struct Stack));

and make it:
newPtr = (struct Stack *) malloc(sizeof (struct Stack*));


//don't you want to connect the rows, with it's columns?

for(i = 0; i < nrows; i++)
   {
        matrix = malloc(ncols * sizeof(char *));  [B]//where's the row number?[/B]
        if(matrix == NULL)
        {
             printf("Out of memory. \n");
             exit(1);
        }
   }
Adak 419 Nearly a Posting Virtuoso

#1 post does not now, have a format specifier for fscanf().

You're saying that it used to have this?

Adak 419 Nearly a Posting Virtuoso

Your fscanf() line of code doesn't have a format specifier:

while ((fscanf(fp, " %s[^\n]", line) != EOF))
Adak 419 Nearly a Posting Virtuoso

Please post up your most recent code. I know you've made some changes, and I'm late joining this thread.

Adak 419 Nearly a Posting Virtuoso

If it's for the screen, you should add some line of like this, inside your for loop:

for(i = 0; i < SomeBigNumber; i++) {
   if(i < 20)
      your print line of code goes here
}

That will let the only the first 20 lines of code be printed.

Changing the console properties isn't the answer - the console can't accommodate a lot of lines, no matter what you do. If you try, you'll only make the text char's so small you can't read them without a magnifying glass. :)

Adak 419 Nearly a Posting Virtuoso

Change the return from your compare function to:

return(*(int*)a->id - *(int*)b->id);

And it works fine. Nice program, btw.

That GD casting on the return from the compare function, is one reason I dislike C's qsort. Sure, it's nice for different kinds of data, but damn it, people sort two things, a great deal - strings and numbers.

It doesn't have to be that much of a PITA. (By itself, Quicksort doesn't need this kind of bull ----).

Adak 419 Nearly a Posting Virtuoso

The size of the original array is set with a #define statement. The secondary array is set in main(), statically. No malloc/calloc is used.

If you add the insertion sort optimization to Quicksort, you will have a much faster sorter, which uses less space than merge sort, of any kind.

I agree completely that a smart (and tested!) optimization, can help any sorting program or function.

Naturally, I've used Google quite a bit, & Wikipedia, to look for C code or pseudo code for Library sort, but come up with nothing but one fortran version. (Which does me no good, since I don't know fortran).

Oh well, I have about 15 sorting algorithms. The surprises to me were:

1) Comb11 - although it's based on bubble sort, this is a powerful sorter. Much better than the regular Comb sort program.

2) Binary Insertion - a nice improvement on straight Insertion sort. Not quite first tier, but good.

3) Quicksort with Insertion on smaller sub arrays - stomps on everything when sorting integers. Radix should be best on strings, but I haven't got to that part of the program, yet.

4) Shell sort - never a first tier sorter in the past, with the new integrated Intel cpu's, it is much improved.

The hardware is so good today, I have to frequently run a sort multiple times, with 10,000 integers or more, to get the sort to take more than 0 seconds. …

Adak 419 Nearly a Posting Virtuoso

This is a lot of information that although old, is still used by Windows (up to XP that I'm sure of).

Also, there is a short example program, using findfirst and findnext.

/* This is from Turbo C/C++ (the C compiler), ver. 1.01 Help

findfirst: Searches a disk directory.
findnext: Continues findfirst search.

Syntax:
int findfirst(const char *pathname, struct ffblk *ffblk, int attrib);
int findnext(struct ffblk *ffblk);

Prototype in:
dir.h

Remarks:
findfirst begins a search of a disk directory by using the DOS system call
0x4E.

pathname is a string with an optional drive specifier, path, and file name
of the file to be found.

The file name portion can contain wildcard match characters (such as ? or
*).

If a matching file is found, the ffblk structure is filled with the
file-directory information.

The format of the structure ffblk is as follows:

struct ffblk {
char ff_reserved[21]; /* reserved by DOS */
char ff_attrib; /* attribute found */
int ff_ftime; /* file time */
int ff_fdate; /* file date */
long ff_fsize; /* file size */
char ff_name[13]; /* found file name */
};

attrib is a DOS file-attribute byte used in selecting eligible files for the
search.

attrib can be one of the following constants defined in dos.h:

Constant ³ Description
=====================================
FA_RDONLY ³ Read-only …

Adak 419 Nearly a Posting Virtuoso

When you are fscanf()'ing a char array, you need to just have the name of the array - that IS a const pointer to the base of the array.

You need to remove the '&' in front of the char arrays that you will be scanning:

while (!feof(fp)){

fscanf(fp,"%d\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",&barc,&aulastname,&aufirstname,&stulastname,&stufirstname,&bktitle,&genre,&date);

if ((strcmpi(search_aulname,aulastname)==0)&&(strcmpi(search_aufname,aufirstname)==0)&&(strcmpi(search_stulname,stulastname)==0)&&(strcmpi(searchbk_title,bktitle)==0)){

Use the '&' for numbers of all kinds, and for char's, but not for strings. Goes for fscanf(), scanf(), sscanf(), etc.

Adak 419 Nearly a Posting Virtuoso

Only for merge sort. The rest of the algorithms do not need a temporary array.
By the way, Happy Holi!

Radix sort will require an auxiliary array, as well.

What I've done for my AllSorts program, is have the second array made equal to the size of the original array. It is not inefficient, that way, and only limits the size of the total array, at a very large size.

There is a version of merge sort that doesn't require any second array, but it's performance is not as good.

You should not be repeatedly allocating memory - that is a substantial slow down to a sorting program.

I am looking for C code, pseudo code, or a detailed description of the "Library Sort", and haven't been able to find it. If you find it, would you post it up, or let me know?

Have you tried optimizing your Quick sort with Insertion sort, when the size of the sub arrays is about 20- 50 elements? It's a big speed up.

Thanks.

Adak 419 Nearly a Posting Virtuoso

I doubt very much if he has a lot of choices right now (after the school year has started), in what compiler the class is using, or what college to attend based on the compiler being used.

Turbo C is no problem, but it sounds like you need to use getchar(), instead of getch(), if you don't have conio.h, or the file is somehow corrupt. This was mentioned above. I can send you the conio.h file, if your file is damaged.

The heart of the matter is doing the conversions. Have you learned how to do these conversions? If so, post up your code to do them, and I'll help you get the flow of your program's menu's, working.

Note that I have no intention of helping you with doing the conversions. If you didn't read your book, or stay awake during the lecture, you're out of luck with me.

Adak 419 Nearly a Posting Virtuoso

Turbo C programs don't run directly under windows. They run in the Virtual DOS machine, which makes the process less efficent.

The VDM isn't included with the most recent versions of Windows either, where 3rd party emulators like DOSbox are the only way run Turbo C programs.

I wouldn't know - it's in WindowsXP, and despite all the "hand-waving" of supremely relevant facts you've made, this is all transparent to the user. And much faster with the current hardware, so I neither know now care how "efficient" it is. :)

I mis-stated something about Gotoxy(), however. You need to add this, to make it work:

void Gotoxy(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
}

and you use it then, like this. . .(just a snapshot of a large function in my chess program)


	Gotoxy(45, 15);				// move list - about 21 moves down and X 3 wide = 63 moves.
	printf("                                 ");
	Gotoxy(45, 16);
	printf("                                 ");

	for(i = 17; i < 41; i++) {
		Gotoxy(45, i);
		printf("                                 "); }

        }

The header you need for this, is windows.h.

Adak 419 Nearly a Posting Virtuoso

Aia is trying to inform you that there is a C/C++ Standard in place. conio.h is not and never has be a C/C++ Standard header, and is only defined in very few compilers. Therefore, use of it and any function defined in it is not portable!

AAMOF, different compilers that have defined conio.h have not implemented the same functions. And sometimes the functions that are defined that have the same name don't even do the same thing.

So, when we 'quote' the Standard it's so the poster can in fact write code that has a chance of being portable. And we will generally gripe about non-standard functions for that very reason. Using your example, with my compiler I get an error using Gotoxy() . And my compiler has conio.h

Of course you got an error, you didn't follow my posts information. :)

For Windows, you have to use Gotoxy(x,y), and include the right header file for it, of course. For conio.h, you have to use gotoxy(x,y), *not* Gotoxy(x,y).

I read and throughly understood Aia's post (although he thinks otherwise). My assertion is that the C standard should have included such a practical header as conio.h, and they should have simply selected a good conio.h file, as a part of our C standard. Then the C purists wouldn't have anything to gripe about when they see it in a program.

Every compiler may not have had the same contents or functionality in their conio.h file, but they …

Adak 419 Nearly a Posting Virtuoso

I found your previous post passionless and somewhat ambiguous other than re-stating the obvious, regarding using conio.h and gotoxy()/Gotoxy(), which are not "undefined" with either the right OS (finally), or the right header files.

Having gotoxy() or Gotoxy() as a part of C, or of the OS, has nothing to do with variable = ++i * ++i or other such syntax nonsense, imo.

Adak 419 Nearly a Posting Virtuoso

What unpredictability?

If you have a Windows OS, Gotoxy() always works. If you have the header file conio.h, gotoxy() always works.

I've used gotoxy() on everything from DOS 3.1 through WindowsXP, using everything from a 386 cpu to an E6700.

No problems. :)

Well, except that the C purists need lots of Pepto Bismol. :) :)

For instance, I wrote a Sudoku solving program. I wanted to show a large Sudoku board, which included the list for each square, of all the possible candidate digits still viable for that square.

I also wanted to show a small board, showing what the computer was working on, right now, in near-real time. Lastly, I wanted to show the original puzzle board position.

Right away, you can't do this without introducing an annoying as hell flicker onto the screen, without using a gotoxy() (either from conio.h or from Window's Gotoxy). **

That isn't what I want. I want to *do* what I can envision for the screen, on the screen, in my programs. I don't want to be told "you can't do that because it's not in the standard, and makes C purists, sick."

Tough. They made themselves sick, they can make themselves well - they'll get over it.

With a single assert, those programmers who are coding for a screen-less system, can eliminate all that conio.h includes, and that would include any gotoxy() code lines.

Somehow, that makes me the devil, …

Adak 419 Nearly a Posting Virtuoso

i am sorry guys..i really don't know maybe because i have about 1 months of experiance in "c"...i will be on the lookout for all of your help...(I gotta admit it you are better then me when it comes to programming)..

Then I would *hope* that we are better programmers, even if sometimes our manners are not quite up to an acceptable level of social etiquette.

C has an odd landscape. In a strong effort to be "machine independent", it jumps all over some very useful bits of code that has been entered into the language, by some older compilers.

Take gotoxy(x, y) for example. Simply locates the cursor in a text window, to a specific row and column, on the screen. This seems to have the effect of making C purists jump right out of their skin, in a rage. So it's dropped, and the header it's in, reviled.

Now along comes Windows OSystem. What do they have - you can guess it can't you? - Gotoxy(x, y). The difference is the G is capitalized, and it's part of Windows, now.

The practical programmer just had to wait 10 years for this from the DOS days, or listen to the "The Scream"** of the purists, or learn some complex method of poking values into the graphics memory of the system. (which of course, varied widely from one type of video mode, to another)


**The Scream is a series of nearly identical famous …

Adak 419 Nearly a Posting Virtuoso

image of ListBox created


[img]
http://www.freeimagehosting.net/uploads/caab9c0bd6.jpg[/img]

OK, I see your listbox pic. Do you have any questions about my previous posts?

Adak 419 Nearly a Posting Virtuoso

I forgot to mention the "auto complete" function.

To simplify matters, I would always have the first letter search, stop at the first alphabetical letter match, it finds, as mentioned in my post above this one.

Then, as each letter is keyed in, the new letter is appended to the target, and the index for the search just continues to increment word by word through the dictionary, until it finds a string that matches the (now one letter longer), target string.

Does that make sense to you?

Adak 419 Nearly a Posting Virtuoso

Your url takes us to Free Image Hosting, but not to your picture/image. :(

You have a large sorted list, and a binary search would be excellent. You'll need a few little tweaks to it:

1) If the person types a 'b', you change that to 'b ', (b + space), so your display to the user, will be from the very top of the alphabetical order of b's, not somewhere down in the middle or near the end.

2) Since it's unlikely that your dictionary actually has a word "baaa", then you want the binary search to give you (display for you), the closest 10 or 20 words, which are immediately after "baaa".

The above is a bit more work than a normal "start at the first word and go right through the list until you reach the b's", but it is elegant, and incredibly fast.

Having said the above, I have to add that I did a database where the search began always in the middle of the database, and then just moved up or down through the records, according to the values it found, compared to the value of the "target" letter or word. On today's computers, I found this approach to be very acceptable when the database had just 3,000 records in total.

For more than 10,000 words in your dictionary, I'd use the altered binary search - especially if access to the records is over a network and it's …

Adak 419 Nearly a Posting Virtuoso

Of course, you don't need an array for min and max, if you just print the column's min and max, at the end of each column's check.

Simple. :)

Adak 419 Nearly a Posting Virtuoso

Is this what you're looking to do?

colMin = array[0][0];
colMax = array[0][0];

for (col = 0; col < MaxCol; col++)  {
   for(row = 0; row < MaxRow; row++)  {
      if(colMin) > array[row][col]
         colMin = array[row][col];
      //and repeat above if statement for colMax

   }
}
Adak 419 Nearly a Posting Virtuoso

Let's look at #1, is the move legal:

Pretty straight forward logic. If the square is empty for the requested move, then the move is legal. Normally a function return value of 0 indicates a "good" return, and anything else is "not good", but we want to turn that around in this case:

So our IsLegal() function will return 1 if it's a legal move, and 0, if it's not.

int IsLegal(int row, int col)   {  //add whatever parameters you need, of course
   int i, legal = 0;
   
   if(board[row][col] == 0)   {
      if(moveNum % 2 == 1)  // X players move 
         board[row][col] = 'X'; //or 1 or whatever you want for first players mark
      else
         board[row][col] = 'O';  //or 2 or whatever you want for the second players mark
      
      legal = 1;
   }
   return legal;
}

//And in our game loop we'd have something like:
if(IsLegal(moveRow, moveCol)
   GameOver();  //Is the game over, now?
...

etc.

If your board array isn't global, then you'll need to send the board array address to IsLegal() (and most of the other functions you'll make for this program, iirc). A copy of the board, won't do.

Adak 419 Nearly a Posting Virtuoso

my only problem now is that when a player attempts to enter a position that has been previously filled.

Bingo! :)

If the array index for the board is != EMPTY, then you can't move there. You can #define EMPTY to be any value you like, including just zero.

So:

1) test if the move that has been entered is legal. If not, reject it and ask for another move.

2) test if the move that you now accept as legal, has ended the game - either by # of moves made (there are no more places that can take a move), or by winning/losing the game.

You'll definitely want to make these functions separate. If you try and do them "straight up" in the game's while loop, you'll flounder from all the logic and lines of code that will be needed.

Also, your merry little program will look like crap from having all this stuff crammed into it, in one place. Not what you want.

Even a simple game like TTT, has a good bit of logic needed for a program to play it. So many things that we humans do "automatically", are completely beyond the computers ability without the most detailed logic being coded up for it.

Adak 419 Nearly a Posting Virtuoso

<<obsolete already>>

Adak 419 Nearly a Posting Virtuoso

Basic error in your logic. You can't say for sure that the game is not over NOW, (after the opponent or yourself, has made a move), unless you check, first. I use a function named IsWin(), but the name is not important.

You must include logic to check if the game is over, and call it, after every move. Counting the number of turns that have been played is OK, but not enough.

Also, I don't see anything to test if a move that is entered, is legal or not. What if the player enters an illegal move? What if the human wants to cheat, you know, like politicians. :p IsLegal() should be called first after every move, then IsWin() is called to see if the last move was a winning move (for either player).

Adak 419 Nearly a Posting Virtuoso

Let's see if I can try to explain myself better with this small test code:

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

int main(void)
{
	int test_num = 216;
	int test_num2;
	char test_string[10];
	FILE *fp_asciiHeim, *fp_converted;

	//create a read/write dummy file
	if((fp_converted = fopen( "data01.txt", "w+" ))==NULL) {
		printf("cannot open file");
        exit(1);
    }

	//fscanf(fp_asciiHeim, "%d", test_num);
	//fscanf(fp_asciiHeim, "%s", test_string);

	// write the decimal value to the file
	fprintf(fp_converted, "%d\n", test_num);
	//fprintf(fp_converted, "%s\n", test_string);

	//reset the cursor to the beginning
	fseek (fp_converted, 0, SEEK_SET);

	//try reading the number either with a string or a decimal
	fscanf(fp_converted, "%d", test_num2);
	//fscanf(fp_converted, "%s", test_string);
	printf("%d\n", test_num2);
	//printf("%s", test_string);
	_fcloseall();

	return 0;
}

So in the code above, I basically write a decimal to a file, and try reading it back. When you use fprintf, it prints the value in ascii; when I try reading it back with fscanf using a %d specifier, I get some random value. However, using %s and a character array I can print out the correct output.

I guess I don't really understand really how fscanf works in terms of scanning for a decimal or a float value. Ideally, I would just like to be able to use %d and get the 216 to store in an int. That would mean that i'm not using ascii anymore to write to a data file. The gist is I don't want to be storing representing the data values in ascii especially when i'm dealing with millions of data points.

which functions/libraries/specifiers do I use …

Adak 419 Nearly a Posting Virtuoso

I want to heartily second Vernon's post - not only is it a great trick for your problem, but it can be used for many others, as well. It's usually called Distributed Counting or Bucketsort, but you don't hear much about it, for some unknown reason.

It has several advantages: 1) It's screamingly fast - far faster than any sorting routine, for instance, and 2) The code is extremely short.

Vernon's description was quite good, but also concise (I guess we know why he likes C, now). :) If you have questions about it, don't hesitate to ask, the technique is sublime and the essence of smart coding, in certain cases.

Adak 419 Nearly a Posting Virtuoso

Yes, we can help, but I see no program to help.

We help - we do not *do* the homework for you.

Salem commented: Well said +19