Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Urjapandya!

Two things:

1) Your program is the same as the program suggested just above your post (Tesu), (but yours is more readable).

2) It would be MUCH better if you had surrounded your code in CODE tags, which makes the forum software keep it looking like a program.

Just click on the [code] symbol, and then paste your code between the two tags that it puts on the editor window for you.

You and Tesu both, have found the more efficient algorithm for detecting palindrome's. Good job! ;)

Adak 419 Nearly a Posting Virtuoso

You're on the right track. If you can't use an array, you can just assign variables for each digit.

Here's an example of how to handle 0-4 digits.

#include <stdio.h>

int main() {
  unsigned long n;   //let's the number be rather large
  int i, zero, one, two, three, four; 
  n=134434;
  zero=one=two=three=four=0;
  printf("\n\n");

  while(n) {
    i = n % 10;
    if(i==4) four++;
    else if(i==3) three++;
    else if(i==2) two++;
    else if(i==1) one++;
    else if(i==0) zero++;
    n /= 10;  //(n = n / 10;  //do this, last in the loop
  }

  if(zero>1) printf("\n I have %d zeroes", zero);
  if(one>1) printf("\n I have %d ones", one);
  if(two>1) printf("\n I have %d twos", two);
  if(three>1) printf("\n I have %d threes", three);
  if(four>1) printf("\n I have %d fours", four);


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

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

You don't mention the most important part of your program - does it work correctly?

Everything else is secondary.

Assuming it does work properly, then what deficiency do you notice or what is it that you want to improve?

With a palindrome program, if it's accurate with all kinds of input, you usually have no worries about the run-time, amount of memory it uses, or anything else. It's a small job, and runs in the blink of an eye.

Say you found a way to optimize the run time so it ran twice as fast - no one would notice or care, unless they strangely had a stop watch and very fast reflexes, right? You don't want to spend time highly optimizing something that can give you no real benefit from.

I thought the overall algorithm was fine, but your posting was not so good. You used tabs in the code you posted, instead of the spaces option, and the comments ran WAY off the line they were intended for. An indentation of 2-4 spaces, is perfect, please use it. Keep the comments either on the line they refer to, or just above it, always.

Adak 419 Nearly a Posting Virtuoso

Then you want to sort the words, alphabetically, not according to the length of the word. We can't have things like:

bat
and
the
cow
the
and

for a sorted word list. We need:

and
and
bat
cow
the
the

and looking at the list, you can perhaps see why.

Now we scan through the word array, and if word[0] is equal to word[1], then that word is not unique. strcmp() will make that comparison for us.

If the word is not repeated (and we again need a flag like outside to keep track of that), then we know that word is unique, in the string.

This is the kind of thing I had in mind:

# include <stdio.h>
# include <stdlib.h>  // for "system("cls");" to clear screen
# include <ctype.h>   // for isalpha(), ispunt()

#define ROWS 20
#define COLS 20
 
void distintwords(char s[]);

int main()
{   
  int i;
  char x, n;
  char s[500]={"Night is drawing nigh. Shadows of the evening, steal across the sky."};


  while(1)
  {
    //system("cls");       
    printf("\nEnter the string: ");
    //fgets(s, sizeof(s), stdin); 
    printf("\n%s\n", s);
    distintwords(s);

    printf("\n\nPress ENTER to try again, 'q' to quit : ");
    x = getchar();
    if (x=='q')
      break;
  }
  i = getchar(); ++i;
  return 0;
 }
void sort(char words[ROWS][COLS], int row) { 
  int i, j;
  char temp[COLS];

  for(i=0;i<row-1;i++) {
    for(j=i+1;j<row;j++) {
      if(strcmp(words[i], words[j]) > 0) {
        strcpy(temp, words[i]);
        strcpy(words[i], words[j]);
        strcpy(words[j], temp);
      }
    }
  }
  //for(i=0;i<row;i++) …
Adak 419 Nearly a Posting Virtuoso

The for loop restricts the number of students being assigned marks. It could restrict it to 0, or to 20000, (or more), depending on what the user input as his/her choice.

Adak 419 Nearly a Posting Virtuoso

Your sorting is used for only integer,i need a quick sort which is generic to all basic types such as int, float, char and struct types

What you are looking for is qsort(), and it's part of the C standard library. The good part of it is that it handles all data types. The bad part is that it is not easy to set up the compare function correctly to handle those void pointers it requires.

This code is not the best example of Quicksort - rather poor actually. It constantly sets the pivot point RIGHT where the pivot point is likely to be a bad value.

Quicksort is VERY sensitive to a bad pivot point - it will degrade to something approaching a bubble sort, under worst case selections, and may crash entirely if the sub-arrays being generated by Quicksort, are not taken smaller one first, at all levels of recursion.

Adak 419 Nearly a Posting Virtuoso

Google it.

Adak 419 Nearly a Posting Virtuoso

No doubt, the int pointer should be given an initial address before you use it!

I believe we used unsigned char's in binary mode, to copy files, back in the Day.

TC was made to comply with the AT&T standard, which was quite good for it's day. Doesn't handle the new stuff, but things like // to rem out a line, are fine.

In my case (ver. 1.01), it's the C++ side of the TC compiler that is nearly useless for today's codes.

Adak 419 Nearly a Posting Virtuoso

One example:

#include <stdio.h>

int main() {
  int i, n ; 
  double d = 0.0;
  d = 3.33 * 3;
  n = d;
  printf("\n N=%d  D=%lf", n, d);
  printf("\n\n\t\t\t     press enter when ready");

  i = getchar(); ++i;
  return 0;
}

Note how anything after the decimal point is truncated.

Adak 419 Nearly a Posting Virtuoso

Are you trying to change a CONST value?

If you add something to the file name, you are renaming the file. If you just want to "touch" some file values, I believe you do that through the FILE pointer struct, associated with that file name.

by "touch" I mean the old "touch" utility - changing file creation time/date, file access time/data, etc.

Adak 419 Nearly a Posting Virtuoso

I'll have to experiment with that - thanks.

Adak 419 Nearly a Posting Virtuoso

Sorry.
char *array[]= {"Lara","Lin","Nitin","Krishna","Sophia"};

Add the asterisk! ;)

Adak 419 Nearly a Posting Virtuoso

You pretty much have it:

char array[]= {"Lara","Lin","Nitin","Krishna","Sophia"};

If you want the names on their own row, then add another dimension to the array:

char array[5][] = { {"Lara"}, {"Lin"}, etc.

Adak 419 Nearly a Posting Virtuoso

Turbo C is smart about how it handles this - it won't just move one int at a time.

I use ver. 1.01 of TC, and I get an error on main() without a return int, but it will allow void main() with no return.

Can you use an int pointer when you want to transfer char's however?

Adak 419 Nearly a Posting Virtuoso

This elminiates the blank line problem:

# include <stdio.h>
# include <stdlib.h>  // for "system("cls");" to clear screen
# include <ctype.h>   // for isalpha(), ispunt()

#define ROWS 20
#define COLS 20
 
void distintwords(char s[]);

int main()
{   
  int i;
  char x, n;
  char s[500]={"Night is drawing nigh. Shadows of the evening, steal across the sky."};


  while(1)
  {
    //system("cls");       
    printf("Enter the string: ");
    //fgets(s, sizeof(s), stdin); 

    distintwords(s);
    printf("\n\nPress ENTER to try again, 'q' to quit : ");
    x = getchar();
    if (x=='q')
      break;
  }
  i = getchar(); ++i;
  return 0;
 }
 
void distintwords(char s[])
{
  int i,j,col,row,wordsN=0,pnctsN=0,spacesN=0,breaksN=0; //no k, etc.
  int count, outside;     //no len, n, or arr[20],
  char words[ROWS][COLS];
                                
  for(i=0;i<ROWS;i++) //sets the words[] to empty - a precaution
    words[i][0]='\0';

  //this loop scans the entire string s, initial state is outside
  outside=1; row=i=j=0;
  while(s[i]!='\0')
  {
    if(isalpha(s[i])) {
      words[row][j]=s[i];
      if(outside) { //new word
        wordsN++;
        outside=0;
      }
      ++j;
    }
    else if(s[i]==' ' || ispunct(s[i]) || s[i]=='\n') {
      if(!outside) {
        words[row][j]='\0';      //add end of string marker 
        outside=1;
        row++;
      }
      if(s[i] ==' ')
        spacesN++;
      else if(ispunct(s[i]))   //must come after test for space char
        pnctsN++;

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

I just put in a fixed string for testing purposes, and added a bit.

Adak 419 Nearly a Posting Virtuoso

Edits are limited by time - after so many hours, the post can't be edited.

The edit button is just above the "Post Reply" button, on the left side of the post itself, if it's possible to edit that post.

By "blank spaces" do you mean a blank line, or just a space?

I haven't looked into blank lines, yet. Spaces were not a problem, however.

Adak 419 Nearly a Posting Virtuoso

I like conio.h, but you should know that's not part of the C standard - it's an extension, and lots of C compilers don't include it.

In those cases, getchar() can replace getch(), conio.h isn't required anymore, and now you're back to standard C.

Thanks for the kind words.

Here's a little bit on arrays. For simplicity, I'm skipping over mention of the end of string char '\0', that would be present for any string.

char words[10][20] is a "true" array with 10 rows, and 20 chars in each of those rows.

Even if the word being saved is "I", that row will still have space for 20 char's in it. Very rigid.

char *words[20] is different. It's an array of 20 pointers to char. Each pointer points to a word of UP TO 20 char's. If you just save the word "I", then you only use ONE char though, instead of 20. Also, the pointers themselves can be swapped around (handy for sorting), without moving any actual words.

To actually move a word, you'd use something like strcpy(). To move a pointer to a word, you just swap the address the pointer has:

Address Word
================
0001 Apple
0002 Cherry
0003 Berry

To sort the words above, I can just change an address:
Address Word
===============
0001 Apple
0003 Cherry
0002 Berry

Which words because we can now refer to …

Adak 419 Nearly a Posting Virtuoso

Let's look at some logic for distintwords():

here, I've added
#define ROWS 20 //and
#define COLS 20 //as well, right below the include files.

I removed conio.h, string.h, and added #include <ctype.h>. Removed some other variables, as well. notes in the code for most of it.

str1[][] I renamed words[][], because variable names beyond k that have no particular
meaning to me, drive me bonkers.


an N appended to the end of a variable name, means it's a Number. row and col inside the function, are not related at all, to ROWS or COLS, of course.

void distintwords(char s[])
{
  int i,j,col,row,wordsN=0,pnctsN=0,spacesN=0,breaksN=0; //no k, etc.
  int count, n, outside;     //no len or arr[20], added outside
  char words[ROWS][COLS];
                             //len = strlen(s)-1;
   
  for(i=0;i<ROWS;i++) //sets the words[] to empty - a precaution
    words[i][0]='\0';

  //this loop scans the entire string s, initial state is outside
  outside=1; row=i=j=0;
  while(s[i]!='\0')
  {
    if(isalpha(s[i])) {     //it's a letter in the range of a-z and A-Z
      words[row][j]=s[i];   
      if(outside) {         //new word
        wordsN++;       
        outside=0;          //not outside any more
      }
      ++j;                  //j is the col of the words[row][col] 
    }
    else if(s[i]==' ' || ispunct(s[i]) || s[i]=='\n') { //end of a word
      words[row][j]='\0';      //add end of string marker 
      outside=1;               //we're outside 
      if(s[i] ==' ')
        spacesN++;             //count it, if it's a space
      else if(ispunct(s[i]))   //must come after test for space char
        pnctsN++;              //or it will count spaces, also.

      j=0;                     //reset column for new word
      ++row;                   //start a new word, on a new row …
Adak 419 Nearly a Posting Virtuoso

Tough luck in the Tour - leaders chain came off at a critical time, and he lost his lead. He's just second in the race, now.

This is what I need in formatting to help with your program. Note that ALL the variables have to be declared at the top of the function - no exceptions.

In general, you had some curly braces { without a matching closing brace. For understanding your code, make sure that the closing brace in the pair, is DIRECTLY BELOW either the opening brace, or the first letter of the line that starts that block of code.

# include <stdio.h>
# include <conio.h>
# include<string.h>
# include <stdlib.h>  // for "system("cls");" to clear screen
 
void distintwords(char s[]);

int main()
{   
  int i;
  char s[500],x, n;

  while(1)
  {
    system("cls");       
    printf("Enter the string: ");
    fgets(s, sizeof(s), stdin); 

    distintwords(s);
    printf("\n\nPress ENTER to try again, 'q' to quit : ");
    x = getch();
    if (x=='q')
      break;
  }
  i = getchar(); ++i;
  return 0;
 }
 
void distintwords(char s[])
{
  int i=0,j=0,k=0,c=0,R=0,len=0,words=0,pncts=0,spac=0,breaks=0;
  int arr[20],count=1,n;     
  char str1[20][20];
  len = strlen(s)-1;
   
  
//This is not complete. It doesn't account for sentences like this,
//where the line ends with a comma, a period, etc., instead of a space.

  while(s[i]!='\0')
   {
     if(s[i]==' ') 
     words++;
     i++;
   }
   
 while(s[j]!='\0')
   {
     if( s[j]==',' || s[j]=='.' || s[j]==';' || s[j]=='?' || s[j]=='!' || s[j]==':' ) 
     pncts++;
     j++;
   }
 spac=words;  //why would spaces equal words?
  //----------------------------------------------------------
    
//initialize R to zero, inside the for loop assignments:
  for(i=0, R=0; s[i]!='\0';i++) …
Adak 419 Nearly a Posting Virtuoso

outside is one of the two states your program can be in, as it scans through the text. Just a "flag", and doesn't need to be capitalized.

When you reach a space (or punctuation mark), you should start saving to a new row, not a new column.

I'll study your code. Watching the Tour de France, just now though.

Adak 419 Nearly a Posting Virtuoso

You already have divided the sentence into each word! When you counted it, remember?

When you find the first letter of a word, you start putting all letters into a row of the wrds array. Keep going until you hit the next space or punctuation char.

Then go to the next row. :)

You'll want a loop for that:

for(i=0;i<ROWS;i++)
  if(word[i] && OUTSIDE) { //add && !punctuation to this
    //it's the start of a new word
    OUTSIDE= 0;
    col=0; //row value is carried into this loop
    wrds[row][col]= word[i];
    col++;     
  }
  else if( //code to handle char's when you are already inside a word ) {
    //col variable is not reset to zero
    wrds[row][col]= word[i]; 
    col++;
    
  }
  //etc.

That kind of thing. Have an errand or two to run, back in 2 hours.

Adak 419 Nearly a Posting Virtuoso

You can have an array that is FIXED in size, and declared looking like an array:

char array;

Or you can have an array that is FIXED in size, but declared looking like a block of memory arranged for an array:

char *array;

The latter one is generally used either:

1) To be cute and smart looking

2) Because the array size is unknown at this time, and the array memory will
be dynamically malloc'd later on.

The key thing is, "under the hood", arrays in C are really blocks of memory, arranged in a particular way. However, it's hard to work with arrays as "blocks of memory...". Non programmers won't lightly tolerate that kind of crap, and you shouldn't work with it that way, unless you're really comfortable with it.

One key concept in programming is to keep your code and logic CLEAR and SIMPLE. If you write your code "oh so cleverly", you (and others), will find it a witch to de-bug, update, or extend the program, later.

<< BE AS CLEAR AS YOU REASONABLY CAN IN YOUR CODE >>

For this program, we need to give it a size, but here's a help:

#define ROWS 100
#define COLS 20

Note, no semi-colons at the end of either line of code!

Put that right under your include lines of code, and then use them to make up your wrds[ROWS][COLS], array.

Any changes …

Adak 419 Nearly a Posting Virtuoso

No. It refers to the number of rows (number of words), you want to process, in total.

SOME_SIZE is not the best name for it, but it IS the size for the first dimension of the wrds[][] array.

Adak 419 Nearly a Posting Virtuoso

Wow! That was great timing.

Your English is fine, no worries.

fgets adds on (if space is available), an end of string char: '\0', to the string. (Because a bunch of char's are just a bunch of char's, without an end of string marker to mark them as a string.)

gets() doesn't include the newline: '\n' char that is generated by the user whenever the enter key is pressed, into the saved string. fgets() does include it, always.

So you may have TWO unseen char's on the end of your fgets() string: a newline and an end of string char. You ALWAYS want that string buffer to be generously bigger than any line of char's you want to save as a string with fgets().

To find the unique words, we need to save each word, and compare it with all the other words. So we'll need wrds[] to become wrds[][20]. That is, it needs to be a 2 dimension char array.

How many words are you going to have to handle at any time, looking for unique words? We need a size for the first dimension of wrds[SOME_SIZE][20].

Then we can find unique words using strcmp(), after we save each word into the wrds[][] array.

Adak 419 Nearly a Posting Virtuoso

gets() is VERY unsafe, since it has the defect of allowing the user to overfill the buffer (potentially crashing the whole program or even taking control of the machine it's running on).

Use fgets(). It works on ANY stream, (file, keyboard, etc.), allows you to limit the amount that will go into a char array, and makes you float better -- maybe! ;)

Think about it, and then write down your steps - how would you count words in text (a string)? Sounds like you have the idea. Would you do it somehow differently for every word, or could you use a loop, and do each word in the same way, using the loop?

What variables would you need? Maybe you can see what I'm getting to here. strtok() is a "water wing" crutch, for this exercise. Save it for later.

I can smell fear right over the internet. ;)

I remember once Lifeguarding for a mile swim, at a pool. A guy in leg braces and using heavy metal arm "crutches", wanted to sign up for the swim.

"Oh", he said "don't worry, I don't use these when I'm in the pool". That was great, because I couldn't let him in with them on - he'd sink like a rock.

"I can swim a mile, don't worry", he added. "I just get horizontal, and propel myself with my arms, and slightly plane up and down to breathe."

I let him swim, …

Adak 419 Nearly a Posting Virtuoso

Sure, but the first thing I want you to do, is to throw your friends code, away. :)

This is an exercise to develop your ability to scan a char array and use logic on it. Forget strtok(), (it's for later), and let's get into top down design:

1) Analyze the problem by doing it by hand (in this case), and jot down the essential steps you use over and over, for each word.

2) Write down those steps, in the order you do them, in small steps of logic, in english.

3) Now, we'll take those steps, and write code to match. Putting off all the non-essential details, like output formatting, etc.

4) Once the essential parts are in place, (functions, and logic), we'll add in the other details, we had put off, previously.

You can't write a program to solve a problem, that you don't know how to solve, yourself. You have to know the steps, and the order of those steps.

Here, you have two states: outside a word, and inside a word, as you scan each char. Spaces and punctuation char's change that state from inside to outside.

You can't learn to swim by standing on the shore, watching others. Forget what the codes of others may do/not do, and let's get YOU into the water.

Don't worry - I'm a Water Safety Instructor. ;)

Adak 419 Nearly a Posting Virtuoso

For me, the above just won't do. I couldn't care less about the "annoying" aspect of the output!

***WHAT*** is the problem that has you stumped --- EXACTLY.

What is your input - how many words do you need to handle. What is your output - what's wrong with it that you can't figure out.

No way am I going to fix some program for your assignment, because it annoys you.
;)

Welcome to the forum, btw. Now get your problem solving hat firmly in place, and get into this assignment's details.

Adak 419 Nearly a Posting Virtuoso
j=-1;
  for (i=0; i<size; i++) {
    if (counter % row == 0) {
      j++;
    }
    pmatrix[i]=pmatrix[i]/ratio[j];
    printf("pmatrix %d after ratio: %f \n",i,pmatrix[i]);
    counter++; 
  }

In the if(counter % row == 0) line, are you aware that when i== 0 (first time into the loop), that the statement will be TRUE, and j will be incremented? Looks like you have it covered with the assignment of j=-1, but I'm not sure.

Finally, you're printing a double, as if it were a float, using %f. You should be using %lf, instead.

Adak 419 Nearly a Posting Virtuoso

@mcamaher:

This thread is over a year old, and your request is likely to simply be buried in all the replies to the original poster.

May I strongly suggest you start a NEW thread, and add in two things:

1) Your effort to make the program, so far

and

2) Your specific problems that are stopping you, atm.

Just posting up for help, and showing no work, gets very little traction in this forum (or most forums for programming).

They don't want to just do your work, for you.

And welcome to the forum! :)

Adak 419 Nearly a Posting Virtuoso

OK, found the answer!

We can't DETECT the graphic driver integer - that sets the graphics system up to the wrong value.

This is the correct program:

#include "stdio.h"
#include "conio.h"
#include "graphics.h"
char far *vidmem=0xB8000000;  
main()
{

int gm, gd=1;  //gd=0 is the white screen that doesn't work, 1-4 are OK (for me)
int r,c,i;
char message[]="praveen";
initgraph(&gd,&gm,"");

clrscr();
for(r=5;r<=20;r++)
{
for(c=5;c<=50;c++)
write2vdu('',64,r,c);
}
c=10;
for(i=0;i<8;i++)     //<=10 goes outside the array
{
write2vdu(message[i],77,10,c);
c++;
}
getch();

}
write2vdu(char ch,char attr,int row,int col)
{
char far *v;
v=vidmem+row*160+col*2;
*v=ch;
v++;
*v=attr;
}

It would be nice to update the main() to int main() and add the return 0; at the end, and also make the write2vdu() function into a void function with a prototype.

Obviously, the memory location (although common), is not going to be "just right" for every system's set up. The display is a brilliant red square (half size of the monitor), with a magenta "praveen" printed on it.

Adak 419 Nearly a Posting Virtuoso

That location in memory, should be for video memory, which the OS sets up for the display card. I did change it around a few times, but just 3-4 guesses, not a systematic search.

I don't know about the part that is here:

v=vidmem+row*160+col*2;

or about the attribute variable. For all I know, it could be writing up just fine, but writing up the letters in white, on a white background, so it's invisible.

I'll spend some more time with it today, but it's not looking really promising, right now. It's close, but close doesn't count for much, in programming.

Adak 419 Nearly a Posting Virtuoso

It's good. ;)

The one I like moves the stream pointer for the keyboard - very elegant, but everybody asks WTF is THAT?

Adak 419 Nearly a Posting Virtuoso

Yeah, I didn't catch the OP date - I saw "Snippet" and it was on the forum first page, and off I went. ;)

Adak 419 Nearly a Posting Virtuoso

Well, phi isn't used in any of the "line" functions, so that can be deleted.

I don't understand why you used 3.1423. Wouldn't 3.1416 be closer?

It's a very nice program.

My critique of it would be that's it's logic is not as simple as it should be. You used good skills to solve the problem, but you didn't use the easiest way to do it.

Aside from having to move some variable declarations to the top of their respective functions, it compiled and ran flawlessly in Turbo C/C++ ver. 1.01.

Congratulations - well done! :)

Adak 419 Nearly a Posting Virtuoso

I can't get it to work, but I can see that it's close. This is with the C compiler in Turbo C/C++ ver. 1.01.

This uses direct video writing, and oddly, direct video writing still works with Windows XP (32 bit), as the operating system. That's for certain!

But I can't *quite* get this program to work as it should. (it should spell out "praveen", of course).

Can you double check with the source code that does work? See what differences there are?

Adak 419 Nearly a Posting Virtuoso

Just print out the numbers at the end of the first for loop, in the select function. That will show the status at the end of each pass, for Selection sort.

Your Bubble sort is wrong, however. Bubble sort is distinguished by always comparing two elements of the array, that are next to each other. Your so called Bubble sort, doesn't do that.

That's the difference between Bubble Sort and Selection Sort - In Bubble Sort, the numbers can never be moved more than 1 element. In Selection Sort, they can, although most of the logic, is the same as Bubble Sort.

So your Bubble Sort just ain't a bubbling, yet! ;)

Adak 419 Nearly a Posting Virtuoso

I didn't mention this, and should have - seems quite obvious, but still:

//j can never be > or == to n. That would be an error, so:

for(j = i + 1; j [B]<[/B] n; j++) //is correct, not j > n
Adak 419 Nearly a Posting Virtuoso

If you don't want question marks messing up your numbers, why put them in char *foo? Your logic and English both leave me with question marks. ;)

Adak 419 Nearly a Posting Virtuoso

First, always highlight your code and click on the [CODE ] icon at the top of the editor. That will put your code into a special font, etc., so it becomes easily readable.

Second, inside your while loop (which looks good by the way), you need to have some variables and maybe use sscanf() or other functions to get the values from the line buffer, into the variables you need to work with. Just printing out the line, won't do the summation you want.

Adak 419 Nearly a Posting Virtuoso

This is Selection sort:

for(i=0;i<n-1;i++)  //OK
{
  for(j=i-1;j>n;j++)  //j=i+1
  {
    if(a[i]<t) //if(a[i] > a[j]
    {
      t=a[i];
      a[i]=a[j];
      a[i]=t;
    }
  }
  a[i]=t;  //delete this line entirely
}

j=i+i is because i<n-1, see? There's always room for j one more than i.

Selection sort is a good sort to memorize, btw. Slightly faster than Bubble sort, and easy to commit to memory, as well.

Check with Wikipedia on your Bubble sort - I never memorized that sort.

Adak 419 Nearly a Posting Virtuoso

If you can't do this, almost without thinking, then it's time you learned how. Really, this is the most basic logic and syntax to learn, after "Hello World".

Get in there! ;)

Anybody who'd do it for you, is doing you NO favors.

Adak 419 Nearly a Posting Virtuoso

the hex value 00 00 00 00 00 BC 61 4E (LSB)

which is stored in string as

char data[8] = {0x4E, 0x61, 0xBC, 0x00,
0x00, 0x00, 0x00, 0x00};

It looks like the char bits need to be swapped, front to back. Is that right? An endian switcheroo.

Then strtol should work.

Adak 419 Nearly a Posting Virtuoso

It's dicey to overwrite data in a file - any error, even a disk error, can ruin the entire file, until the file is rebuilt. That is a time consuming process, which may not fix all the data, anyway.

So what to do?

Most programs do it this way: When a record should be deleted, you don't actually delete it, you just mark the record ID number field (or maybe a name field), with a 0 (zero). The record space is still there, but it won't be shown on any searches or print outs. You make adjustments in those functions to ensure that is the case.

Also, your program should have logic to let it know to look for a zero'ed out record, when it needs to add a record. If the program detects too many empty records, (it counts them on start up each time), then it will compress them, at a convenient time (maybe the middle of the night, or when you try to close it down, next time). The idea is that instead of shifting all these records "in place", it will simply write all the good records into a new file, and then re-name that file, to it's current file name.

Do you see the difference? Instead of 25 overwriting operations, each involving moving say, half the records in the data file each time, you will have *maybe* one re-writing operation, which can take place at a time of your choosing.

Ancient Dragon commented: Excellent :) +31
Adak 419 Nearly a Posting Virtuoso

use of a string did solve the telephone number problem and use of gets resolved the space issue.. but I still dont know what is wrong with the use of clrscr() .. How do I clear the screen before the other question is asked? is there any other way so that repeated use of clrscr() can be avoided?

Sure.

Although Turbo C will include gotoxy() for you, it's still good to have this, at the top of your program:

#include <conio.h>

Now it's easy to put a blank area, anywhere you want:

gotoxy(RowYouWant, ColumnYouWant);

which puts the cursor where you want it to be, then:

printf("                                                                    ");

print a bunch of empty spaces, where you need them.

Anyone trying to run your code without conio.h built into their compiler, will need to change that line to use the Windows API for setting the console cursor position, or use the curses/ncurses library.

Adak 419 Nearly a Posting Virtuoso

Yes, but the dos.h functions WORK in WindowsXP, as does conio.h.

Adak 419 Nearly a Posting Virtuoso

dos.h functions work fine in WindowsXP and Windows2000 (both 32 bit), but the compiler has to support those functions. It appears that Dev-C++ does not do that.

Turbo C supports all the dos.h functions that I've tried, over the years. (I have Turbo C/C++, ver. 1.01). It's a free download at the legacy Borland download site - you may want to use it, instead of Dev-C++, to get those functions working. It has conio.h included, so you don't need to worry about using curses at all.

Adak 419 Nearly a Posting Virtuoso

Add
#include <string.h>

to the above. My compiler does it automatically, so I forget to add it sometimes.

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Sumanawal. :)

More details are needed.

You can't put a graphics image into a console (text) window, but you can adjust the background and text colors, in a console window.

Are you trying to work with a graphics window, or a console window?

Adak 419 Nearly a Posting Virtuoso

If you're using conio.h, you have no need for curses, and vice-versa.

Decide which library you like (conio.h) <cough, cough>, and forget the other. ;)

Adak 419 Nearly a Posting Virtuoso

I wouldn't go that way with your logic. Don't use ascii numbers for your letters, unless you really need to. Use char's for letters, and stick with 'A', 'B', etc., instead - much easier to read and intuitively understand.

A couple things to get figured out:

1) You need to find the largest char to set as your remove char, in case the instructor (or you), wants to do this with a larger string that goes up to 'G' or beyond.

2) You'll need to print up strlen(c)/2+1 rows. So there's an outer for loop.

3) Your inner for loop

#include <stdio.h>


int main() {
  int i, j, len; 
  char c[15] = { "ABCDEFGFEDCBA" };
  char remove = 'A';

  printf("\n\n%s", c);
  len = strlen(c);

  for(i=0;i<len;i++) {   //find the greatest char
    if(c[i] > remove)
      remove = c[i];     //set it as the remove char
  }
  for(i=0;i<len/2+1;i++) { //for each row to be printed
    for(j=0;j<len;j++) {   //check each char in c[]
      if(c[j] == remove)   //and if it's the remove char
        c[j]=' ';          //replace it with a space
    }
    --remove;              //decrement the remove char
    printf("\n%s", c);
    
  }
  
  printf("\n\n\t\t\t     press enter when ready");

  i = getchar(); ++i;
  return 0;
}

;)

Shikhin commented: Thanks +0