Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Chrysanterus!

Always better if you start a new thread for a request.

I've zipped up all the example programs that I got with Turbo C. You can d/l the self extracting file from Swoopshare, here:
http://en.swoopshare.com/file/71f8ef6d5a664112a30d0c420ec58218/TCexamples?tags=TCexamples&lang=en

note that this is from an early version. Since then, gets() and flush(stdin), have been deprecated. Use fgets() and getchar() (to pull the newline char off the keyboard buffer after a scanf()), instead.

If you google for C tutorials, you'll find some good one's - including long video's on beginning and intermediate C, from some good universities (Stanford, etc).

Adak 419 Nearly a Posting Virtuoso

Sorry Libathos, that sounds a little too virus-like/malware type programming, for me.

"Beware the Dark Side of the Force", Libathos.

Adak 419 Nearly a Posting Virtuoso

Google for "algorithms in C", for starters, and check out Wikipedia's algorithm's, as well (they have LOTS).

Flowcharts are harder to find, (just not that popular on the internet, I believe), but give all your search engines a shot with that subject matter, as well.

"All", you will never get, but you can get a lot! ;)

Welcome to the forum! If you have any other questions like this, be sure to ask them in a new thread, not somebody else's thread.

@Ayoub:

Hopefully smarter members will give you an answer, but the question is "how far did you get?", and "what has you stumped, now?".

Nice pseudo code, btw. Logic is very clear, I just don't know what the bleep you're referring to! Somehow, in my one semester of programming, this was glossed over! ;)

Adak 419 Nearly a Posting Virtuoso

I don't know who helped you with the code previously, but they were quite good, imo.

Questions for you:

My compiler is C89, and won't handle this style of variable declarations. Can you set your compiler to format the code for C89 style declarations in your options menu?

If so, please do that.

Since your current code looks so good, at what point is it not doing what you need? I was going to start from almost scratch, but now that I see the program more closely, there's no reason to do that.

Can you list what the current program does RIGHT from your numbered list? And if you can't re-format the code, I can do it by hand.

TTYou Soon. Thanks for posting the program in code tags - I can study it a lot better.

P.S.
In File #1:

Record #1: field #1 < >  field #2 < > field #3 < >
Record #2: field #1 < >  field #2 < > field #3 < >
Record #3: etc.

Each row is another record, and field numbers start with 1 or zero, again.

Adak 419 Nearly a Posting Virtuoso

1.Yes all the msr files are in the same folder in the USB key directory.
2.The OS is linux,compiler is gcc, IDE is debian.
3.On data, Your right its the first field as you highlighted above.
4.Mean is calculated for each msr data seperately.(ie for all the msr data, calculate the mean in each file) thats what will be written to the file with thir respective dates.
5. Their lengths are close

Yes, I am practicing my counting <j/k> ;)

1) You know how to compile and run a simple program on your system? I ask because your linux distro is Debian, not your IDE, and I have not used gcc myself.

2) Separately is good, that makes it easier.
3) I'm always right -- c'mon! (Except when I'm not) ;)
4) Two means get written to the output file per intake msr file. How many of the #1 field's do we write out to the output file?

What's the format for the output?

Field #1: <which date??> Mean#1: <double var> Mean#2: <double var>
??

First and last dates, maybe?
From: <first date>

To: <last date>

Mean#1: <double var>

Mean#2: <double var>

??

Each output file will have just a very small amount of data, then?

Oh! Before I forget, when you post any code, be sure to put it between [code] tags, so the forum will keep the code looking …

Adak 419 Nearly a Posting Virtuoso

You'll certainly want to open the files in binary mode.

First thing I'd do is write a function that will open and copy a solo and regular JPG file, and make sure it can be then viewed OK.

Then, I'd see about getting the smallest *.RAW file I could, with only two small JPG's in the file, and see if you're able to separate out the two JPG files. Then use your function above, to copy them out to their own files.

And again, check that they are OK, when viewed.

The file naming is simple - no problem there.

#include <stdio.h>

int main() {
  int i;
  char filename[] = {"file0.jpg"};
  char n = '0';

  for(i = 0; i < 3; i++) {
    filename[4] = n++;
    printf("\n New filename is: %s", filename);
  }

  n = getchar();
  return 0;
}

Let us know how you're doing, OK? And Welcome to the forum! ;)

Adak 419 Nearly a Posting Virtuoso
Adak 419 Nearly a Posting Virtuoso

The backslash char is an escape char in C. It's paired up with another char to represent something difficult to type: like \n for a newline char, or \t for a tab, etc.

Two backslashes is used when you want to delineate just one backslash itself.

Adak 419 Nearly a Posting Virtuoso

You need to remove the return of float from your function (make it void), and remove the print in main, asking for a string.

gets() is NOTORIOUSLY unsafe - use fgets() instead.

And indent your program, so it makes sense to read it. It's a mess* this way. The closing brace '}', should be directly underneath (in the same column), as either the opening brace '{', or the first letter of the code that begins the loop:

for(i=0, j=1107;i<MAX;i++) {
  printf("\n%d", i);
  printf(" %d", ++j);
}

for(i=0, j=1107;i<MAX;i++) 
{
  printf("\n%d", i);
  printf(" %d", ++j);
}

Either of the above is a good style. Easy to read, and easy to spot bugs in logic or syntax.

*Mess is being kind.

Adak 419 Nearly a Posting Virtuoso

Good.

For #1, will the msr files always be found in the same directory, or do we need to search for them, on the flash drive?

What is your operating system for this program, and what compiler and IDE are you going to be using?

For the data:

The first column is '2', in each record. If you need the date, please refer to it as the first field, instead of the first column.

I confuse easily. ;)

The fields for one record are below, and you need the fields that are bold typed, right?

2010-03-07 00:00:11; 70.68; 15.8; 5.66; 0.00; 86; 9.47; 9,55; 1,089; 48,5; 221,59; 372,6; 16.4; 0

On the mean calculations, do you need the mean from one msr file's data, or from ALL the msr files data?

Do the msr files vary in length a great deal, or are they close to being the same size, each time?

And #10 you can cross off - directories or folders are just locations, and don't need to be closed.

And you're welcome. ;)

Adak 419 Nearly a Posting Virtuoso

My suggestions:

1) change the value for alive or dead to 1 or 0. The printout can easily show a dot for 0, and 'X' for alive.

2) Delete the defines in red.

3) Instead of 'V1', etc., name your directions something you can intuitively associate with the 8 directions: maybe D1-D8, or D1, D3, ... D12. Indicating the approx. hour hand of the clock, (with you in the center), as it faces that direction.

Definitely, arrange your D's, so they work in order, from 12 or 1 o'clock to 10 or 12 o'clock, in a clockwise direction. Why clockwise? It's in ascending order, by hour. And I say so. ;)

4) I would prefer the D's was an array, rather than 8 different variables. Now you just count up the neighbors status, with a for loop:

for(i=0,alive=0;i<8;i++,D++) {
  if(grid[D]])
    alive++;  //alive is an int
}

Unfortunately, I don't know a clever way to make D increment to E (and thus become a new direction in a define statement), in C. Defines don't handle that in compiled languages, AFAIK.

So you're left with 8 if statements:

if(grid[D1] alive++;
if(grid[D2] alive++;
//...

if(alive>2)  //do something

Long involved defines are a witch to work with, down the road. Keep them short, direct, and clear.

Try to avoid being too clever. Whoever works on your program next, will have to be even more clever to extend or debug it, making it difficult indeed.

Adak 419 Nearly a Posting Virtuoso

Actually, // for comments has worked for C in old Turbo C (my version is 1.01), and now is part of the C99 standard.

Yes, it will be longer, since one of the child nodes must be "moved up", to become the new parent, and be "attached" to the grandparent node. (the p pointer points to the grandparent, in the delete function).

The other child node becomes a child of the new parent.

To avoid long term "left single" or "right single" threads, the choice of which node should be moved to parent in this case, should alternate. A rather fine point, to be sure, only for long term BST use by a program.

Yes, I've been reading! ;)

Adak 419 Nearly a Posting Virtuoso

The path to the BGI drivers.

Adak 419 Nearly a Posting Virtuoso

You'll find this much easier if you use a bit of top-down design here, especially:

Put Off The Details

Get the basic control and flow working, then get your details going, LATER.

You have a source file, and it has 30 names of other files in it, or what? Because your source file won't have 30 other files, in it. ;)

please get in the habit of always putting your code between [code] tags - otherwise your code, won't look like code, at all. The forum editor will turn it into html text, and it's ugly to study, then. The code tags icon is at the top of the editor window.

Prepare and post a 5 line sample file, in a properly formatted source file, so it can be studied. Include 5 msr files, also (just small one's). Your descriptions are helpful, but not what will be needed.

Make a list of what your program needs to do, from start to finish:

1) Open the master file
2) Read all the other msr filenames
3) Open each msr files
4) extract data 1 (specify the data)
5) extract data 2 (and specify it again-details are important to know, but not code, now)
6) list the computations you need to make on the data (mean, etc.)
7) output to file certain data (list specifics)
8) close msr files
9) close master file

Short, and punchy. …

Adak 419 Nearly a Posting Virtuoso

Did you use the whole delete function? Because there are a few other changes to it, as well.

Yes, the delete function now deletes the root, IF t==top, in the program. Ran it 5 times - worked every time. Did not test with negative numbers, however.

There may be a problem with nested comments:

/*

/* */

else
*/

being handled differently on our compiler's. I see the forum software doesn't allow them (the else in my post above is not green, and should be).

Let me see what's up with that.

What numbers failed for you?

Delete lines 45 through lines 56 (which is just my doodling with some code anyway), and then re-run it. It still works fine for me, how about you, now?

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum!

You obviously need to add some logic here - hardly a surprise that month needs to be set to 1, along with the day, at the end of the year:

if((day == 31) && (month == 12)){
         printf("The date of the next day is: %d/",nextday);
         printf("%d/",nextmonth);
         year++;
         printf("%d",year);
   
         return 0;
       }

You need to get your head into the calendar, and your program. These errors are low hanging fruit, just waiting for you.

Don't get tired, and don't get lazy - get in there!

Adak 419 Nearly a Posting Virtuoso

The problem with deleting the root was that we're working with t, but "top" is the real boss address, and it is a global.

So this is the logic to handle that, warts and all:

void delete(struct tree *t, int num)
{
   struct tree *p;  //the parent of t  
   /* locate the key */
   while(1) {
     if(t==NULL) {
       printf("\n Value not found");
       return;
     }
     else if(t->data<num) {
       p=t;
       t=t->right;
     }
     else if(t->data>num) {
       p=t;
       t=t->left;
     }
     else if(t->data==num) 
       break;
   }  
   
   /* node has no children */
   if((t->left==NULL) && (t->right==NULL)) {
     if(p->left==t)
       p->left=NULL;
     else
       p->right=NULL;
//     t=NULL;
//     free(t);
   }          
   /* node has 1 child */
   else if((t->left && !t->right) || (t->right && !t->left)) {
     if(t->left) {
       p->left=t->left;
     }
     else {
       p->right=t->right;
     }
     t->right=NULL;
     t->left=NULL;
//     t=NULL;
//     free(t);
   }

   /* node has two children */
   /*  888888888888888888888 Working here! 88888888888888888888888
   else {
     if(p->left==t) {
       t->data=t->left.data;
       /* to be continued ;) */

     }
     else 
   }
   */

   /* is node the root? */
   if(t==top) {
     t=top=NULL;
     free(t);
   }
   else {
     t=NULL;
     free(t);
   }
}
Adak 419 Nearly a Posting Virtuoso

I have some important errands to run, just now. I'll take a look at this when i get back (2 hours).

If the root is not being deleted, then it appears that either the left or right child has not been set to NULL, as it should have been,

OR

t is never being set to the true root.

Back after bit. Let's keep our Deerstalking caps, close at hand:

"Come, Watson! The game is afoot!" ;)

Adak 419 Nearly a Posting Virtuoso

This is another of those "your assignment is to eat, but you can't touch the food with anything", kind of an exercise. ;)

Which means the lecturer will have given you hints (maybe subtle, maybe not), about how he/she wants you to proceed with this.

Review your class notes, and remember what those clues were, for a first step.

Second step, is to post up your try at this problem. Be specific about what the problem is that has you, or your program, stumped. "Won't work", on a post from you, won't work with us.

Adak 419 Nearly a Posting Virtuoso

By "project", do you mean a "final year" type of programming project, or do you mean a project inside your IDE (your compiler)?

And welcome to the forum! ;)

Adak 419 Nearly a Posting Virtuoso

Surprised to still be up, but RL is odd that way.

I don't like the 12 and 6 printing - you can't easily move them to another column on the screen.

If you want your numbers to line up, you should use %2d, instead of %d, with ALL the numbers being printed. Right now, your picture shows the 11 overlapping the 0 in the 10, and 11 is also too far away from the 12. The one is too far away from the 12, also.

The two and the three are in the same column, so move the 3 to the right.

The six is too far away from the seven, and also from the five. You can space it out right without the %2d format, but it is MUCH more difficult.

Make that change and see what it looks like. Run my version, and you should be able to see the differences in spaces (if any exist then), very quickly.

Adak 419 Nearly a Posting Virtuoso

@Rje7, I'm getting beat up by the sandman, so I have to hit the sack. I couldn't work with your program because you have your variables declared all over the function - my compiler won't accept that.

By printing two numbers wide, I meant in a field 2 digits wide, like so:

printf("%2d", 8);

where 8 could be any single or double digit number. That's a neat trick to know for all kinds of formatting output, btw - even works for files.

I'll check back tomorrow. Hit me up if you have any questions.

Adak 419 Nearly a Posting Virtuoso

You defined your struct, and made a pointer to it, but I don't see an actual struct being declared.

Also, scanf() needs a getchar() after each instance, if you want to keep getting char's from it. Need to pull the newline char, off the keyboard buffer.

Adak 419 Nearly a Posting Virtuoso

After working on this problem, I like your code much better than I did before. ;)

Seriously.

I did mine with two while loops, but in my haste, the logic is not as elegant as it could be. For a clock face, I can accept it, though. :D Everything lines up.

#include <stdio.h>

int main() {
  int i, j, median, offset, rows, n;
  n = 12;
  median = 15;
  offset = 3;
  rows = 7;
  printf("\n\n");

  i=0;
  while(i<4) {         //handles the top half of the clock face
    for(j=0;j<median-offset;j++) { 
      putchar(' ');    //prints left margin spaces
    }
    printf("%2d", n);    //then the number
    if(i==0) {
      putchar('\n');
      --n;           //get ready adjustments
      ++i;
      offset+=3;
      continue;
    }
    for(j=0;j<2*(offset-4);j++) {  
      putchar(' ');  //prints center spaces on clock face
    }
    printf("%2d\n", i);   //and the second number of the row
    offset+=3;      //get ready for the next loop
    --n;
    ++i;
  }//end of while

  offset -=rows-1;
  while(i<rows) {      //handles the lower half
    for(j=0;j<median-offset;j++) {
       putchar(' ');   //first the spaces on the left
    }
    printf("%2d", n);  //then the number
    if(i==rows-1)
      break;
    for(j=0;j<2*(offset-4);j++) { 
      putchar(' ');    //then center spaces
    }
    printf("%2d\n", i); //and the second number of the row
       
    offset-=3;         //and adjust for the next loop
    ++i;
    --n;
  }
  printf("\n\n\t\t\t     press enter when ready");

  i = getchar(); 
  return 0;
}
Adak 419 Nearly a Posting Virtuoso

For a square clock (on edge), just keep everything lined up, and allow for ALL the numbers, to have 2 digits, even if they only have one:

As far as a squared off clock face goes I prefer this one:

12
            11    1
         10          2
       9               3
         8           4  
            7     5
               6

over the original. The gap between the 6 and 7 is more uniform.

12
          11     1
       10           2
     9                3
       8            4
          7      5
              6

A graphics console is a graphics console, whether in Turbo C or not. The problem becomes the resolution of that graphics console - in Turbo C, the resolution will be rather low, at best, because the graphics drivers available for TC, were all much lower than what we have today.

Still, it's much better than we can do in a text console! ;)

But this looks like it was meant to be done in a text console, and it's a an exercise in using logic, not graphics commands.

So, I'll look at your code, and see what's up with it. Back in an hour, real life stuff to attend to right now.

I have noticed your code uses very little logic, and lots of "magic numbers". Those are numbers just added into code that seem to have come from the sky or by magic. Let's use variables, and logic, and as few magic numbers as possible.

Adak 419 Nearly a Posting Virtuoso

There are three cases that have to be dealt with when you remove a node:

1) Node has no children (leaf node)
2) Node has one child node attached. Child must be attached to the parent of
the node being deleted
3) Node has two child nodes. Both nodes must be attached to the parent of
the node being deleted

This (hopefully!) covers the first two cases. Case #3 is basically the same as case
#2, so I'm leaving that one, for you.

I see why you had problem with this - every bit of info I could find on it, either glazed over the details, (especially for #1), or showed the details in C++ or Java, in OOP style programs.

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

struct tree
{
   int data;
   struct tree *left;
   struct tree *right;
}*top,*a,*b;

struct tree * make(int m)
{
   struct tree *newnode;
   newnode=(struct tree *)malloc(sizeof(struct tree));
   newnode->data=m;
   newnode->right=newnode->left=NULL;
   return(newnode);
}

void left(struct tree *t,int x)
{
   if(t->left!=NULL)
      printf("\n Invalid!");
   else
      t->left=make(x);
}

void right(struct tree *t,int x)
{
   if(t->right!=NULL)
      printf("\n Invalid!");
   else
      t->right=make(x);
}

void inorder(struct tree *t)
{
   if(t!=NULL)
   {
      inorder(t->left);
      printf("\t %d",t->data);
      inorder(t->right);
   }
}

void preorder(struct tree *t)
{
   if(t!=NULL)
   {
      printf("\t %d",t->data);
      preorder(t->left);
      preorder(t->right);
   }
}

void postorder(struct tree *t)
{
   if(t!=NULL)
   {
      postorder(t->left);
      postorder(t->right);
      printf("\t %d",t->data);
   }
}

void locate(struct tree *t, int num)
{
if ( t==NULL )
   {
      printf("\n\n The number is NOT found!");
   }
   else
   {
      if …
Adak 419 Nearly a Posting Virtuoso

Minor woops! on my above post. Virtual Keys detects key presses, but if you want to know if a key is being HELD down or not, you need getasynckeystate. Also at the MSDN library.

Adak 419 Nearly a Posting Virtuoso

This is your code, changed to put the add a number, inside the menu. No help on delete yet, need to clear some more time for that one.

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

struct tree
{
   int data;
   struct tree *left;
   struct tree *right;
}*top,*a,*b;

struct tree *make(int m)
{
   struct tree *newnode;
   newnode=(struct tree *)malloc(sizeof(struct tree));
   newnode->data=m;
   newnode->right=newnode->left=NULL;
   return(newnode);
}

void left(struct tree *t,int x)
{
   if(t->left!=NULL)
      printf("\n Invalid!");
   else
      t->left=make(x);
}

void right(struct tree *t,int x)
{
   if(t->right!=NULL)
      printf("\n Invalid!");
   else
      t->right=make(x);
}

void inorder(struct tree *t)
{
   if(t!=NULL)
   {
      inorder(t->left);
      printf("\t %d",t->data);
      inorder(t->right);
   }
}

void preorder(struct tree *t)
{
   if(t!=NULL)
   {
      printf("\t %d",t->data);
      preorder(t->left);
      preorder(t->right);
   }
}

void postorder(struct tree *t)
{
   if(t!=NULL)
   {
      postorder(t->left);
      postorder(t->right);
      printf("\t %d",t->data);
   }
}

void locate(struct tree *t, int num)
{
if ( t==NULL )
   {
      printf("\n\n The number is NOT found!");
   }
   else
   {
      if ( t->data==num )
         printf("\n\n The number is found.");
      else if ( t->data<num )
         locate(t->right,num);
      else
         locate(t->left,num);
   }
}

void delete(struct tree *t, int num)
{

}

int main()
{
   int num, choice, loc;
   char ans;
   clrscr();
   printf("\n Enter the root: ");
   scanf("%d",& num);
   top=make(num);
   a=top;
   do
 	 {
      clrscr();
      printf("\n        MAIN MENU\n");
      printf("\n       [1] Traverse");
      printf("\n       [2] Locate");
      printf("\n       [3] Delete");
      printf("\n       [4] Add a Number");
      printf("\n       [5] Exit");
      printf("\n\n Enter the number of your choice: ");
      scanf("%d",&choice);

      switch(choice)
      {
      	 case 1: clrscr();
    	      printf("\n\nPreorder:  ");
    		    preorder(top);
    		    printf("\n\nInorder:   ");
    		    inorder(top);
    		    printf("\n\nPostorder: ");
    		    postorder(top);
    		    getch();
    		    break;
      	 case 2: clrscr();
            printf("\n\n Enter the number to locate: …
Adak 419 Nearly a Posting Virtuoso

You opened the file for writing. I'm guessing it should be closed before you call CopyFile.

Adding code tags (click on the [code] icon at the top of your editing window), improves your code's readability, greatly.

Adak 419 Nearly a Posting Virtuoso

Time is usually measured in C with two clock_t variables:

#include <time.h>
#include <stdio.h>
#include <dos.h>

int main(void)
{
   clock_t start, end;
   start = clock();

   delay(200);  //adjust as needed for your compiler and OS
   //sleep(200);  //ditto

   end = clock();
   printf("The time was: %f\n", (end - start) / CLK_TCK);

   return 0;
}

This is old Turbo C code. Change CLK_TCK to suit your own compiler macro (may be TICKS_PER_SEC, or ??)

Throughput is just units / time to process. What units would you suggest for your program?

Adak 419 Nearly a Posting Virtuoso

Check out your help index for "Virtual Keys" or "Virtual Keyboard". If you have nothing there, go to the MSDN library - it is definitely there, with an example program, as well.

Adak 419 Nearly a Posting Virtuoso

Obvious errors.


If you want a nice round clock face, you need to work with a graphic mode console, not a text one, and then use math.h to calculate the number of degree's or radians needed between numbers.

If you want a square one like the red one at the top of your post, you can make that, using the text based console, if you work it out right.

What is the problem with getting the output to look like the squared off red numbered one? Is that what you want, or do you want a round (normal) clock face?

Adak 419 Nearly a Posting Virtuoso

difficult!

Algorithms are just general guides, they have to adjusted for every specific problem. There are programs that do this, for a very limited environment, and limited algorithms, but they are a very big project.

Not something for the beginner.

Adak 419 Nearly a Posting Virtuoso

Let me study that article, but it looks very doable. Back later today.

Adak 419 Nearly a Posting Virtuoso

Easy does it Ellenski. If Ancient Dragon recommends something, I'd read it. He's one of the most knowledgeable posters you'll find.

I, on the other hand, know just about zip about BST's, but clearly you need to adjust the left and right pointers to the nodes that are linking to the node you want to delete. Otherwise, the tree would have a "sinkhole" in it.

Sight unseen, I'd bet you $$$ the article he mentioned, has the answer to that very question.

Why don't you read that article, and I'll do the same. If you're still stuck, post back and say what has you stumped, and what you tried, already.

;)

Adak 419 Nearly a Posting Virtuoso

You wind up comparing a char, to a string in words - that won't work.

In your call to findchar(), I'd just pass the one word that you will be checking, rather than the array. You seem to be confused about that, so:

words[random_word][] should do.

ARRSIZE sounds wrong, also. You have ROWS and COLS in a 2D array, and the ROWS * COLS equals your array size.

Then you need to step through the word, checking words[random_number]== guess, one char at a time. (incrementing i)

And welcome to the forum! ;)

Adak 419 Nearly a Posting Virtuoso

You read my post, but not really studied it.

Study my previous post, and and this one, and think of your arithmetic. What times what is negative? What times what is positive? How could that be used in your case, as mentioned previously?

Adak 419 Nearly a Posting Virtuoso

I am NOT going to tell you this answer - you would hate that, surely.

if(hint, hint), EITHER the multiplicater, OR the multiplicand, (but not both!) are negative, what will you have to put at the very end of your function, to make the logic correct?

C'mon! ;)

Adak 419 Nearly a Posting Virtuoso

The fastest way to do this, is to:

1) Get your basic FLOW and calculations of the program, working correctly. At first, just display it on the screen - it's faster, and so easy to go back AFTER you have the basics right, and change the printf() statements, to fprintf(), and just add the file pointer.

2) Check your equation out on Google - I'm sure a basic equation like that is all over. Whatever numbers you get at first, check it and make sure it's right, before you do much else.

3) Post up your skeleton program, minus the final details, if you get stuck and need some help. Check each function as you code it, for syntax and other errors, before going to the next function.

Once you get the basics working, the details just fall into place nicely.

Adak 419 Nearly a Posting Virtuoso

You're on a roll with your troubleshooting - don't stop now!

Add a putc(cur) into the loop, and see what's going on. Ancient Dragon's suggestions are excellent, but you will HAVE to develop your troubleshooting skills, no matter how good a programmer you ultimately become.

Dig in there, be a stubborn badger, and find out what is going wrong. Trust me, you WON'T forget it, anytime soon, AND you will have learned a valuable less in how to troubleshoot code. Keep narrowing down the possible problems until you have it isolated and know it EXACTLY.

BE SURE that your compiler is a C compiler, and not a C++ compiler. That's an option in a lot of compiler's. They have both C and C++ bundled together in one product.

Adak 419 Nearly a Posting Virtuoso

No idea what's wrong with it. I know what's wrong with this type of program development, though. What I see is:

1) If you want people to study your program, you need to put it between CODE tags, on the forum. Click on the CODE icon in the editor top bar, and paste your program between the tags it provides. Otherwise it looks like html text, and not a program.

2) Listen to your compiler - is it giving you any warning?

3) Learn to troubleshoot your program. Remove (/..../) calls to functions until you know which function has the error. You can do that, as well as anyone else.

4) Follow the steps in #3, to troubleshoot the lines of code in the troubled function, until you know which line of code is causing the crash.

5) Now study that line, and ask questions about THAT line of code, in your program.

6) This is made much easier if you build your program, and check it out, function by function - for accuracy and dependability. If it's all one big function, substitute "block of code" for the word function, here.

7) Some logic that looks fishy:

if (cur != '\n' && cur != '\0')
{
count2++;
}

What about periods and comma's and such? Check out the ispunct() function. You'll need to include ctype.h header file.

The first thing I would do is put in some print statements, and getchar()'s, right at the end of some blocks …

Adak 419 Nearly a Posting Virtuoso

It will compare the two strings:

strcmp(string1, string2);

If the return value is greater than 0, string1 has sorted higher than string2. If the answer is anything less than 0, then string2 has sorted higher.

string1 > string2, return > 0

string1 < string2, return < 0;

You can remember this, by seeing how the > symbols associate, in the above two lines.

Adak 419 Nearly a Posting Virtuoso

Assign the largest number initially, to the value of the first element of the array. Assign the 2nd largest number initially to INT_MIN (your compilers macro for the least possible value for an integer.

That makes the "mouth" for trapping the numbered values you want in the array, suitable.

(Your compiler may call it MIN_INT or something different, but you can check in the header file, limits.h, and you should include it).

If you get "stuck" in a really sticky logic situation in this problem, you can always just sort the values in the array, say descending, and then count down by one, for every new value in the array. That way duplicates don't goof it up.

Adak 419 Nearly a Posting Virtuoso

This is Turbo C guaranteed! ;)

/*Here is the code I wrote up to solve your problem.
c Syntax (Toggle Plain Text)
*/

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   char two_digit[3];
   int digit;
 
   printf("Enter two digits: ");
   fgets(two_digit, 3, stdin); //Accept size-1 (in this case, 3-1 which is 2) bytes into the buffer.
   digit = two_digit[0] - '0';  //zero, not Oh
   printf("The first digit is: %d\n", digit);

   printf("\n\n\t\t\t    press enter when ready");
   while((digit=getchar()) != '\n');
   digit=getchar();
   return 1;
}
Adak 419 Nearly a Posting Virtuoso

When you declare nested structs, you can expect some redundancy in your code.

I'm thinking use something like:
#define PARENT and then put in all the outer structs and fields you need, so only
PARENT->YourSubStuctureFieldName would be all you need to access that nested field or struct.

How does that sound?

Adak 419 Nearly a Posting Virtuoso

That is a different problem than what you posted, above. Extracting m characters, beginning with the nth character, means just that. It does NOT mean "scan the char string for some char and when found, extract m characters".

If you want to post code, PLEASE click on the [code] tag icon in the editor window, and put your code between the two code tags that it give you.

That keeps your code, looking like code, and not html text.

Adak 419 Nearly a Posting Virtuoso

I don't believe there is a problem doing this, you're not bringing your pointers or member operator on down to the right level.

Using the dot operator, I would say:

parent.nameOfField.nameOfChildStruct.fieldIwant

You can do the same idea with pointers, of course.

Adak 419 Nearly a Posting Virtuoso

fgets() DOES limit the amount of chars you can put into your buffer, but you never made your collection of chars in your buffer, into a legitimate string - it has no end of string marker.

#include <stdio.h>

int main() {
  int i; 
  char buff[25];
  buff[sizeof(buff)-1]='\0';   //setting the end of string marker

  fgets(buff, sizeof(buff)-1, stdin);

  printf("\n\n%s", buff);

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

  i = getchar();
  return 0;
}

When you print a collection of char's that aren't terminated with an end of string marker, you can get a lot more char's than what are actually in your buffer. Tricky, eh? ;)

Welcome to the forum, and PLEASE surround your code with CODE tags (click on the [CODE] icon in the editing window, to get a pair).

Adak 419 Nearly a Posting Virtuoso

When you see code like this, why not simplify it?

if(n == a[i])
  {
   pass++;
  }
  else if(n > a[i])
  {
   low = i+1;
   pass++;
  }
  else if( n < a[i] )
  {
   high = i-1;
   pass++;
  }

to just:
  pass++;
  if(n == a[i])
  {
    return the i;
  }
  else if(n > a[i])
  {
   low = i+1;
  }
  else if( n < a[i] )
  {
   high = i-1;
  }

Shorter, simpler, and more clear.

Adak 419 Nearly a Posting Virtuoso

You are quite the politician! In one paragraph, you say the compare function is not difficult to write for qsort().

On the very next paragraph, you state that you're unable to write the compare function for qsort(). ;)

I don't use qsort(), because I prefer other versions of quicksort. This is what I use for integers, when I use qsort():

//for integers:
int compare_int(const void *a, const void *b) {
     return(*(int*)a - *(int*)b);
}

I consider this compare function difficult to code up correctly, "on the fly".

(also, my Quicksort versions are quite a bit faster then qsort)

This is the compare function for strings using qsort(), btw:

//for strings
int compare_str( const void *a, const void *b) {
     return(strcmp(a,b));
}

This is the best Quicksort version I've found and tested for run time. It's simple, clear, unoptimized, but fast:

//Quicksort w/o a separate compare function :)
void quicksort(int A[], int lo, int hi) {
  int i, j, pivot, temp;

  if(lo == hi) return; 
  i=lo; 
  j=hi;
  pivot= A[(lo+hi)/2]; 

  /* Split the array into two parts */
  do {    
    while (A[i] < pivot) i++; 
    while (A[j] > pivot) j--;
    if (i<=j) {
      temp= A[i];
      A[i]= A[j];
      A[j]=temp;
      i++;
      j--;
    }
  } while (i<=j);
    
  if (lo < j) quicksort(A, lo, j);
  if (i < hi) quicksort(A, i, hi);
}

To optimize it by about 12%, change the "if(lo==hi) return" statement near the top of the code. Have it call an insertion() sort function, if the …