Adak 419 Nearly a Posting Virtuoso

<< beat me to it >> :)

Adak 419 Nearly a Posting Virtuoso

You made me laugh with your post to convert decimal to hex, including "A,B...G"
*G* ?? :D

Anyway, your algorithm to change decimal to hexadecimal is wrong. Here's a modded version of your program, which shows what the hex value of a decimal, should be:

#include <stdio.h>

int main(void)
{
   int hexa = 16, deci, quotient, ctr;
   printf("enter decimal number: ");
   scanf("%d",&deci);

   for (ctr = 1; ctr<=deci; ctr++)
      quotient = deci / hexa;

   printf("Equivalent in Hexadecimal is %d",quotient);
   printf("\n\n Try this for Hexadecimal: %X", deci);   

   getche();
   return 0;
}

Good luck with your studies.
Adak 419 Nearly a Posting Virtuoso

I believe this will work better. Note that you were writing out the variables in a slightly different order than you were fscanf'ing them back in, and the failing message shouldn't go into the data file, because you have no way of handling it when the data is read back in.

Instead, just add an if statement to your program after the data is read back in from the file, and then print the "student has failed and can't promote" message, onto the screen.

#include<stdio.h>
#include<string.h>
#include<conio.h>
#pragma warning(disable: 4996)
 
float avg(float x);
int main()
{
    FILE *fp;
    int k,students=10,subjects=4,i,j,id_number,fail;
    float marks[100],total=0,average;
    char name[30],course_enrolled[30],filename[10],date_of_birth[30];
       //printf("filename\n"); 
       //scanf("%s",filename);
    printf("\n\n\n\n");

    fp=fopen("filename","w");
    for(i=1;i<=10;i++)
    {     
        printf("Enter Name of Student\n");
        fscanf(stdin,"%s",name);
        fprintf(fp,"%s\n",name);
        
        printf("Enter Course In Which Student Is Enrolled\n"); 
        fscanf(stdin,"%s",course_enrolled);
        fprintf(fp,"%s\n",course_enrolled);
        
        printf("Enter Date Of Birth\n");
        fscanf(stdin,"%s",date_of_birth);
        fprintf(fp," %s\n",date_of_birth);

        printf("Enter ID Number\n");
        fscanf(stdin,"%d",&id_number);
        fprintf(fp," %d\n",id_number);

        for(j=0;j<4;j++)
        {
            printf("Enter Marks for Subject\n");
            scanf("%f",&marks[j]);
            total=total+marks[j];
            if(marks[j]<50)
                fail=fail+1;
//            else
//                continue;
        } 
        average=avg(total);
        total=0.0;
        printf("AVERAGE=%f\n",average);
        fprintf(fp," %f\n",average);
        if(average>=70)
            printf("Grade A\n");
        else if(average>=60&&average<70)
            printf("Grade B\n");
        else if(average>=50&&average<60)
            printf("Grade C\n");
        else if(average>=45&&average<50) 
            printf("Grade D\n");
        else if(average>=40&&average<45)
            printf("Grade E\n");
        else if(average<40)
            printf("Fail\n");
//        else 
//             continue;
        
        
        
        
        if(fail>=2)
           { 
                   printf("Student is failed and can't promote\n");
                 //fprintf(fp,"Student is failed and can't promote\n");
           }        
//        else
//            continue;
        fail=0;

    }
    fclose(fp); 
    fp=fopen("filename","r");
    for(k=1;k<=10;k++)
    {
           //printf("Input filename"); 
           //scanf("%s",filename);
        //fscanf(fp,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,&id_number,date_of_birth,average);
        fscanf(fp,"%s %s %s %d %f",name,course_enrolled,date_of_birth, &id_number,&average);
        fprintf(stdout,"NAME= %s\tCOURSE= %s\t ID= %d\t DOB= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);

       if(average < 40)
          printf("\n*This student has failed and can't promote*\n"); …
Adak 419 Nearly a Posting Virtuoso

In main() you call the menu() function. The menu() function has a very large do while loop in it that usually starts with ** Welcome to ...... **" kind of banner at the top.

Inside the menu(), you'll present the choices, ask the user to choose which one they want, and then you'll have a large switch statement that will guide the program to the right functions depending on the user's choice.

int main(void)  {
   menu();
   return 0;
}  
void menu()   {
   int i, choice, etc.

   do   {
      print "Welcome to My Program's Main Menu", whatever
      print "Please select from the following choices"

      1) Deposit
      2) Cash Withdrawal
       ...
      9) Cancel or Quit

      etc.  (Your menu choices looked better than mine :) )
      scanf("%d", &choice);  //fgets() is much better but I understand books 
                             //don't usually teach with it.
      switch(choice)   {      
         case 1: call your functions to handle Deposits; break;
         case 2: call your functions to handle Cash Withdrawal; break;
         etc.
         default: print I didn't understand that choice; break;
      } //end of switch
   }while(choice != 9);

}
Adak 419 Nearly a Posting Virtuoso

I don't want to modify your code. I want you to get smart enough to modify your own code.

If you can write out one record, you can write out a million records, but you can't write out records using incorrect data type specifiers. For instance, date of birth is an array, but you're trying to write it out as an int "%d". That won't work!

Why haven't you checked over these simple errors and fixed them by now? They're very basic, but you haven't done that.

Go through your program and find out specifically what does not work, and ask specific questions about it. If you don't know about something, ask about it, again specifically. We can't read your mind, and have no desire to re-write your code from top to bottom or replace a C programming text book. This is just a forum.

You're going to have to get a lot more actively involved if you want to get this program done right. There are three foundations to programming: Study, Work, and Practice. They will help you, pleading to God will not.

Adak 419 Nearly a Posting Virtuoso

What compiler are you using?

Does it have a "conio.h" file? Does it have a "window" or similar command to create a text mode window?

Because as I'm typing in your program's window, the letters don't "wrap" to the next line, they just go right off the window! :(

If you have a window command, use it, and include the "conio.h" header file, if you have it. It includes commands to shortcut this type of program: clreol (clears all text from cursor to the end of the line), etc.

Adak 419 Nearly a Posting Virtuoso

When you press enter on 4th row, the letters on the 4th row, should be moved up to the 3rd row. The cursor will still be on the 4th row (because that is your bottom row), and that 4th row will now be all blank spaces ' '.

I believe that a "stationary" display array, rather than a "windowed" display, will be much easier to work with.

Adak

Adak 419 Nearly a Posting Virtuoso

ok, thanks. But plz help me in reprint the array on the same loaction in the screen . Plz iam anewbie ...plz help me to understand ,,, plz specify a example with small code..plz

Posting up my editor would do you no good. I was a student when I wrote it, and it's in another language.

You can reprint the array via code like this:

for(row = 1; row < 5; row++)   {
   gotoxy(1, row);
   for(col = 1; col < 20; col++)
      putchar(screen[row][col]);
   putchar('\n');
}

This is done after you've made your changes in the screen array, of course.

This is also off the cuff, I didn't try to run the above. I believe it's OK, however.

I'm having a lot of trouble trying to understand what the real problem is that you're stuck on. You're going to have to post your whole program so I can run it, and see what is wrong or right with it.

Adak 419 Nearly a Posting Virtuoso

I don't see any differentiation between the cursor being on the last line, and the cursor being on some other line of the screen.

It is impossible for me to tell what's wrong without the full code, so I can run it and see what's happening as it runs.

Normally, like the forum editor here, all the char's to the right of the cursor are moved down to the next lower row on the screen, when Enter is pressed. That area will then be filled with blank spaces, the end of the row. All the rows below the cursor will be pushed down on the screen, by one row.

So your logic has to handle both the char's to the right of the cursor when Enter is pressed, and also handle all the rows of char's which are below the cursor. Lastly, it must move the cursor itself, down one row, and over to column 1.

Now, after the char's have been moved in the screen array, each char will need to be printed, in it's new location.

It's always helpful if you indent your code, properly. It makes it much easier to spot logic problems, particularly in nested loops. Also, I would *not* use row 0 or column 0. When you're dealing with a screen, which has no row 0 or column 0 on it, that will always goof you up. Use row 1 and column 1 and then it's simple, since gotoxy(1, 1) …

Adak 419 Nearly a Posting Virtuoso

damn, dude. lighten up already. this place isnt for you to vent your frustrations on some hapless student for asking legitimate questions. Yeah, perhaps he framed his question a little oddly (it did seem like he wanted code) but there is nothing wrong with asking questions on how C functions work.

because that's the purpose of this place: for students (and professionals) to ask C programming questions. Now if you dont have time to answer these questions, that's your business. don't answer them, okay?

but maybe you should spend more than a month here before you decide who this place is and isn't for.
.

He's hapless, or just acting helpless? His help file, his man file, his book, his notes, google, etc., have no info on continue, break, or return?

Hahahahaha! :D :D

Would you like to answer more homework questions for the student, without him even trying to answer them, himself?

I have no problem with answering a question about C. I have a problem when the student doesn't even try to explain what he/she thinks a continue or break, or return, could be used for, or post any snippet of code relating to any of these statements.

When it comes to teaching, I've been doing it since before you were born, Jepthah. You have to strongly encourage a good work ethic in a student, if they're to be their best.

If this forum is to be it's best, it needs to …

Adak 419 Nearly a Posting Virtuoso

Yes, the text on the screen will only scroll when you have the cursor on the bottom row of the screen, and press Enter. It doesn't matter where on the bottom row, just anywhere, on the bottom row.

And I showed you code to help do that. (the two for loops)

Your array should be a two dimension array, like screen[row][col]. Row will equal 4 in your case, and col will equal the width of your program's screen, in char's.

If the user presses the delete key, you move all the other char's over 1 column to the left. Of course, that effects all the rows below it, as well, so they also shift over 1 column to the left. The char on column 1 goes up one row on the screen, to the last column in the higher row on the screen.

If the user presses the space bar, you move all the char's to it's right, and below it on the screen. All these char's move one column to the right. Char's at the last column, need to go down to the next row. All the char's below it will be shifted in this way.

Adak 419 Nearly a Posting Virtuoso

I believe there are 2 sub cases when the user presses Enter:

1) If the cursor position is above (less than) the bottom row of the current screen (usually 25, but may be 43 or 50), then the cursor just moves to the next row down, and to the first column of that row. No rows are appearing at the bottom, or disappearing from the top of the screen.

2) If the cursor position is on the bottom row of the screen, then everything on the page should move upward, and a blank row should be added to the bottom of the screen. Again, the cursor is moved to the (new) bottom row, and to the first column of that row.

You may move the rows upward 2 ways:

1) by having an absolute array # of rows for your screen. (1-25 maybe)
2) by having a "windowed" array of rows for your screen. Here, the top row of the array (etc.), will change. It may start with 1, but when you press enter, the top row becomes 2, and the bottom row becomes 26, see?

Which way do you want to move the rows upward, and what specific problem are you having trying to do it?

for(r = 1; r < 26; r++)
   for(c = 1; c < 81; c++)
      array[r-1][c] = array[r][c];

Where r = row, and c = column, and row 0 and column 0 are not used, which works …

Adak 419 Nearly a Posting Virtuoso

This line of code is correct:

index_s1 = strlen(num_s1) - 1;

Without the " - 1" in there, you are going outside the bounds of the array. Arrays are zero based, so if the array has 12 elements, and you access array[12], of a char array, you will access an end of string char '\0', (if the compiler made it during initiation, ie:
char array[] = "How are you?";

will have strlen of 12 char's, 0 - 11. The array[12] will be the end of string marker char: '\0'.

If you filled the char array yourself, it may just have junk there, but anyway, the -1 in the above string, makes all the difference.

Adak 419 Nearly a Posting Virtuoso

This isn't the place for "can you tell me about some feature of C". This is *not* a lecture hall, and we don't have the time to be unpaid tutors.

You have to remember, there are a *lot* of students who post here.

Post your code, and tell us your problem with it - we'll try to help. That's what we do, and that's about all that we have the time to do.

I'm sure you can google just about any subject or question to find tutorial material on the features of C.

jephthah commented: you need to get over yourself -1
Aia commented: Everyone makes mistakes, no need get personal +8
Adak 419 Nearly a Posting Virtuoso

When you run a program and the first choice shows nothing, it's safe to say it's lacking! :)

I re-worked Choice A, but the answer is being written out in reverse. You need to take the digits and first put them into a small array or LIFO buffer of some kind, and print them out from that array or buffer in last in, first out, ourder, so when the number is 3, the zero's are all printed out before the 1's.

Always highlight your code you want to post on the forum, and press the # sign, so it will be made to look more like code, and not regular forum text.

#include <stdio.h>


int main()  {
   int ctr, bin, quotient, deci=0, binary,octal,hexa, gar;
   float rem;
   char mark_magic;

   do   {
      clrscr();
      quotient = 1;
      binary = 1;

      gotoxy(32,3); printf("-=< MAIN MENU >=-");
      gotoxy(15,5); printf("from any Number System to Decimal System conversion");
      gotoxy(5,6); printf("------------------------------------------------------------------------");
      gotoxy(5,7); printf("------------------------------------------------------------------------");
      gotoxy(26,9); printf("[A] Binary System");
      gotoxy(26,11); printf("[B] Octal System");
      gotoxy(26,13); printf("[C] Hexadecimal System");
      gotoxy(26,15); printf("[X] Exit");
      gotoxy(26,21); printf("what's your choice?: ");

      mark_magic = getche();
      clrscr();
      switch(toupper(mark_magic))   {
         case 'A':
         clrscr();
         gotoxy(10,5); printf("########^_^ Convert Decimal system to Binary system ^_^########");
         gotoxy(5,6); printf("------------------------------------------------------------------------");
         gotoxy(5,7); printf("------------------------------------------------------------------------");
         binary = 2;
         gotoxy(28,9);printf("enter decimal number: ");

         scanf("%d",&deci);
         quotient = deci / binary;
         gotoxy(28,11);printf("Equivalent in Binary is ");
         for(bin=1; bin <=16; bin++)  {
            printf("%d",deci % 2);
            deci = deci / 2;
         }

         gotoxy(51,24); printf("press any key to exit: ");
         getche();
         clrscr();
         break;

         case 'B':
            clrscr();
            gotoxy(10,5); printf("########^_^ Convert Decimal system …
Adak 419 Nearly a Posting Virtuoso

Every student of C should know that scanf() is very "fragile" and should only be learned because it's a part of the language, and every book on C seems to use it.

scanf() should thereafter *never* be used, instead use fgets() and send the input to a buffer than can't be over-run.

It takes 10 minutes to learn fgets(buffer_name, size_of_input_you_want, pointer_to_source (could be stdin) ), and once you learn it, you'll be *so* much better off than you ever could be using scanf().

Adak 419 Nearly a Posting Virtuoso

guyz im having hard time of making a program that could run like a simple ATM machine using turbo c(using conditional statement and looping)... I can't think will coz the dealine of submission is on next week... could you gave an idea of that program.. pls??? i will just change some concepts to make it more better... im having hard time in conditonal i dont know how to use coz im just starting to learn turbo c.... but i found it quite fun.. hehe...

You need to write down your ATM options on each scrren, before you can program anything meaningful.

Welcome - Please Enter Your ATM Card, and Your PIN.

Then:
Options:
1) Withdraw Cash 2) Deposit Cash 3) Check Balance

etc.

Post up all this as part of your menu() function, with each option being another small function. That would work well.

Show us some code, and some real work on this, or you won't get much help, imo.

Adak 419 Nearly a Posting Virtuoso

:( giving error
program has encountered a problem and needs to close. we are sorry for inconvenience..............send report....
tell me what to do now !

This is incorrect:

fprintf(strea, "%s %s %d %s",&name[10],&course_enrolled[10],&id_number,&date_of_birth[10]);

Try:
fprintf(strea, %s %s %d %s", name[i], course_enrolled[i], &id_number, date_of_birth[i]);

//The name of the array is an address to the base of the array, so array's need no & in //them, here.

In your first for loop, you need to do (as I understand your program), three things:

1) get input, either from file, or from the user, using fscanf(), or scanf().
2) print the data onto the screen with printf()
3) write the data to a file, with fprintf().

EACH part of the input loops may need a printf() and fprintf() line of code in them.

I don't see that in your program.

Just post your compiler errors or warnings. Operating system warnings do no good - but almost always refer to your program trying to read or write data somewhere it shouldn't, in memory.

Also, this is wrong. You open the file for reading, and then you are writing to the file:

strea=fopen("STUDENT.FIL","r");
        fprintf(strea,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);
        fclose(strea);

Open the file for append or writing, or writing and reading, if you want to write to it.

It's after 2:00 a.m. here, so I have to go to bed. Good luck. You need to study harder …

Nick Evan commented: Good help +7
Adak 419 Nearly a Posting Virtuoso

You've opened the file for reading, not writing, and your fprintf line is wrong. Do it like this:

//example for writing to a file
int main(void)
{
   FILE *stream;
   int i = 100;
   char c = 'C';
   float f = 1.234;

   /* open a file for update */
   stream = fopen("DUMMY.FIL", "w+");

   /* write some data to the file */
   fprintf(stream, "%d %c %f", i, c, f);

   /* close the file */
   fclose(stream);
   return 0;
}
Adak 419 Nearly a Posting Virtuoso

I am a beginner trying to write a program for my intro C class. I am trying to write a program that calculates the combination of numbers given a specific output by the user. The number has to be between 1-10 to be valid. I am not allowed to use the built in library that calculates a combination. I am getting stuck at the part where i need to calculate (n-k)! I see that my problem is after my loops the numbers the user inputs for n and k are now zero. I am not sure if I should have written this a different way or how I should go about getting the original entered digits back so that I can take and use them in calculating (n-k)! Any help would be very much appreciated.

int main()        /* Main declaration with arguments           */
{

int n,
    nFact = 1,
    k,
    kFact = 1,
    n_k = ( n - k ),
    nkFact = 1,
    combination;
   
do {
 printf("\nEnter the number of items in the list (n): ");
 scanf("%d", &n);
       if ( n < 1 || n > 10 ) /* is n between 1 - 10 */
          printf("\n?Invalid input: Number must be between 1 and 10\n");
       else  
          break;
} while ( n > 1 || n < 10 );
//the while test is goofed. If n < 1 || n > 10, THEN you want to loop back.
 
  /* As long as n>=1 program will loop the following n*n-1 */
  while …
Adak 419 Nearly a Posting Virtuoso

Just
system("sample");

in a loop, will do. You may need to add a sleep(# of seconds) statement, before the next loop, so your bat file will have time to run, before the next call to run it.

Do you know if the commands inside the sample.bat file will actually send emails or not? That should be a first test, imo. Then you can time what you should have for a sleep in seconds. The time won't always be the same, so give your program some extra time.

Adak 419 Nearly a Posting Virtuoso

No, I'm not going to try and run your code to find out what the problem(s) may be. I'm going to wait for you to tell me what *specific* problem(s) you have found with the program.

It is your program, after all, not mine.

Is the input wrong? What's wrong with it?
Is the output wrong? What's wrong with it?

It takes time for even a professional programmer to discover your program's problem(s), let alone a hobby programmer, like myself.

Time is valuable, you should respect that.

Adak 419 Nearly a Posting Virtuoso

Is this right? (Line 75 or so)

if (element > element [left+1])   //???

{

   temp = element[left+1];   

   element[left+1] = element;   //???

   element = temp;   //???

}

element is the *address* of the array, a pointer to the array, and it's being compared with a value of the array??

You should be getting a warning about this from your compiler. You shouldn't compare an address with an integer or char value, nor should you try assigning the address of the array (which can't be assigned), the value of a char or array, or even another address.

Adak 419 Nearly a Posting Virtuoso

First, I would change the variable "int o;" to some other name. Never use "o" or "O" for variable names, because they look too much like 0.

Second, in your description of the problem, you are moving in the wrong direction. You start with the *smallest* column's number, and work up to the *biggest* column numbers. That way you know how to manage the carries.

Third, I would initiate your arrays with all zero's, and that's you're starting place as you add up your numbers.

It looks to me like using char's is confusing you. Switch your arrays to int's, instead. For simple adding, I don't see why you need nested for loops. Not at all.

while(digit >= 0)   {
   j = i; 
   num1 = number1[i];
   num2 = number2[i++];
   digit = num1 + num2;

          //"carry" could very well be it's own little function
   if(digit > 9) {  //it has a carry
      j++;
      number1[j]++;
carry:
;
      while(number[j++] > 9)   {
         number[j]++;
         goto carry;
      }
   }
  i++;
}

The above is all untested, and just for a springboard for your own code. It's not probably ready to compile & run. :)

Adak 419 Nearly a Posting Virtuoso

Line 571 has this code:

printf("Edit this record?(Y/N)");
scanf("%s", answer);
if(strcmp(answer, "Y")==0)

Which I believe should be changed to:

scanf("%c", &answer);  //answer is a char, not a string
if(answer == 'Y')      //note the change to a single quote for a char, instead of double quotes
   etc.

You have the same problem with the variable "answer" on line 491 in your student delete function.

Adak 419 Nearly a Posting Virtuoso

hi cld anybody explain me the concepts of continue,break,return statement with an example using c program

Sure. Then we could write your text book, your website tutorial, take your tests, etc.

I don't mean to be harsh, but we're not Google, we're not auditioning to be your tutor, and we're not a replacement for your text books.

Life is way too short for that. We did our homework or individual studying, and you should do yours. Don't take us for granted, please.

The way it works is YOU post your code or pseudo code that you're working on/having trouble with or don't understand, and we'll try to help you solve THAT problem, on your posted code.

jephthah commented: everyone makes mistakes no need to get personal. -1
Adak 419 Nearly a Posting Virtuoso

i got lost in your codes >.< i mean im really not that good, i'll study it! is there by any chance i might be able to convert chars to ascii? aside from the implicit conversion provided by c. . %c --> %d?? i'm making the program be able to accept chars as well. .

Sorting is not a terribly easy subject to tackle. Needless to say, I didn't just "whip" up this code in a few minutes. :)

If you want the sorting program to handle char's, instead of int's, then just make the arrays into arrays of char's, and change mid (where it's assigned the value of MAXINT), to mid being assigned the value of MAXCHAR, instead. Mid needs to be changed to type char; that's all I can think of that needs to be changed, right now.

If your compiler doesn't have a MAXCHAR value, you may need to set it as a define MAXCHAR 512 (or 256 if you use unsigned char's).

Naturally, you need to change the printf type to %c instead of %d, so printf can give you what you want.

I am now working on a large programming project, and don't have the time to re-do the program. I did give it a bit more testing today with 20 & 21 numbers, and it worked great.
You should test it much more before relying on it, however.

If you have other problems, just post them up with …

Adak 419 Nearly a Posting Virtuoso

oh thanks a lot on that, i'm trying to make it accept letters and convert it to ascii to be arranged T_T thanks a lot.. maybe if you'd someday be here in the philippines i'd treat you ^_^

Letters are just small 8 bit numbers, and they are already ascii in value, (on most PC's at least).

This is a program that shows the idea's I was thinking of. You'll need to modify it to suit your own purposes, AND you'll need to test it thoroughly. I've only tested about 15 different number combinations, in quantity from 2 through 11.

The rest as they say, is left as an exercise for the student. :)

//Adak's help for SwiftDeath 

#include <stdio.h>
#include <values.h>
#define Size 11

void insertSort(int a[], size_t length)  {
   int i, j, value;
 
   for(i = 1; i < length; i++)  {
      value = a[i];
      for (j = i - 1; j >= 0 && a[j] > value; j--)  {
         a[j + 1] = a[j];
      }
      a[j+1] = value;
   }
}
int main(void)  {
   int i, j, k, mid;
   int a[Size] = {0,1,9,3,5,2,0,8,4,6,7};  //9,3,2,0,8,4,6,7};
   int left[Size/2]= {0}, right[Size/2] = {0};
   
   mid = MAXINT;

//   insertSort(a, sizeof(a)/sizeof(*a));

   putchar('\n');
   for(i = 0; i < sizeof(a)/sizeof(*a); i++)  //same as i < Size
      printf(" %4d", a[i]);
   getchar();

   i = 0;
   while(i < Size/2)  {

      //increase the numbers by one in both the first and last subsets
      left[i] = a[i]; 
      right[i] = a[Size - Size/2 + i]; 

      //sort both the left, …
Adak 419 Nearly a Posting Virtuoso

You didn't post the program - you posted "something like it".

So it's not just a snippet of code which shows the problem, and it's also not the whole program.

I'm not going to waste time with it. Your quote:

(if there is any undeclare variable just ignore.. it's because i forgot here, but it's all in my program)

Adak 419 Nearly a Posting Virtuoso

Hi Everyone,
I'm really fed up of working on C in Windows Vista.
First and foremost, the window of C occupies only half the screen which is very annoying.
I've heard that there is a distinct C that is used with Vista but I have failed to attain any more information about that 'C'. Please help me out.

I use two IDE's for C/C++. Both work very well. The first is the old standby Turbo C/C++, and the second is the much newer Visual Studio 2008 (free edition).

The Turbo IDE can be very small if you have the wrong properties for it's display window. If that's what you're using, let us know and we'll tell you how to easily fix it so it's full screen.

The Visual Studio free edition is full screen, by default.

So, give us the details of your IDE for C, and we can get this fixed.

Adak 419 Nearly a Posting Virtuoso

I'm working on a version of this. It won't be exactly like your version above, but it will help you along and give you some idea's, I'm sure.

Because of the holiday, it won't be posted until Sunday, some time.

Adak 419 Nearly a Posting Virtuoso

i need help on my homework problem.

i have done the operations part (+, -, *, /, sqrt) but i am really having a problem on making my calculator accurate up to 40 digits and to truncate all the non-significant zeroes on the output. i don't know what data type to use since i know that the highest decimal place the program can give me is only up to 6 decimal places.

can anyone help me write the codes needed for this? i am really having a problem on it right now. thanks a lot!

Stop using the built in data types, since obviously they will do you no good for 40 digits of accuracy. :) Remember the base 10 number system basics, and that char's are just 1 byte
numbers that work just like the small int's that they are (less than 256 or 512 depending on
whether they're unsigned char's or signed char's).

Try this:

printf("\n %c", 65);
printf("\n %d", 65);
printf("\n %c, 'A');
printf("\n %d, 'A');

Truncating the non-significant zero's is easy to do when you have your own display function of your new array of char's:

accuracy = -1; dotFlag = i = 0;
while(a[i++] == 0);  //runs the index past the leading zero's before the first digit
i = 0;
while(1)  {   //now print the real digits
   
   if(a[i] == '.')  {
      dotFlag = 1;
      putchar('.');
   }
   else
      printf("%d", a[i]);
   
   if(dotFlag)
      accuracy++;

   if(accuracy == accuracyYouWant)
      break;

   ++i;
}

You have to …

Adak 419 Nearly a Posting Virtuoso

so far, it performs the 1st pass correctly and produces the desired output for ascending order, although what it does was a wee bit different, the right and left subsets sorts the numbers within them and won't swap it.. >.< i don't know what to do, hmm.. maybe you're right, but i got to get to school first T_T maybe i'd sleep when i get home after class

I think I see where you're coming from with this algo. This is why it violates a long held, near and dear tenant of algorithms.

You have a big problem - in this case sorting, now let's see how one of the best (and one of the fastest) sorting algo's handles this problem.

Take 20 numbers, pick a mid point. Move all the numbers greater than this midpoint into a new left group. Take all the numbers that are greater than the midpoint, and put them in a new right sub group.

Now in one of these sub groups, pick a new midpoint - and again, move all the numbers less than the midpoint to a new left side subgroup, and all the numbers greater than the midpoint to the new right side subgroup.

Continue like this until you have just 2 numbers left in the subgroup. Compare them and swap as needed.

(it now returns to all the other (larger) sub groups and repeats the above).

The point is Quicksort has taken a large problem, …

Adak 419 Nearly a Posting Virtuoso

I'll look at it later tonight. I'm not surprised because it's really an odd algo, and we're having to wing it a bit, as we go.

You get some sleep.

Adak 419 Nearly a Posting Virtuoso

wow you're really awesome! ooh, the last or final pass wouldn't actually sort the whole set of array rather compare both subsets and swap if they fulfilled the condition. .

OK, I see what you're talking about. That would not work, I believe. That relies on the numbers having a favorable distribution, to work. Making the final pass a merge pass, would work, though The left subset is sorted, the right subset is sorted, so a merge pass would just be very efficient and logical, at this point. And it would work with any distribution of numbers.

Go to Wikipedia (or anywhere), and read up on merge sorting. That would be the ideal last pass, here.

Let me know.

Adak 419 Nearly a Posting Virtuoso

Hi can anyone help me, I am learning the language C but need some help writting some code. what i need to do is from the input of the keyboard i need to find the max and min of some values and out put them. what i got so far is:

// Program to select a Maximum and Minimum from inputted numbers.

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

int main ()
{
int numtimes;
int tempnum1, tempnum2, tempnum3;
char indicator = 'y';

clrscr();

do
{
printf("\nEnter a number ");
scanf(" %d", &tempnum1);
//assign max and min to tempnum1, here

printf("\nEnter a second number ");
scanf(" %d", &tempnum2);
//add the two testing if statements, here

printf("\nEnter your thrid number ");
scanf(" %d", &tempnum3);
//add the two testing if statements, here as well

printf("\nWould you like to continue? (enter n to exit) ");
scanf(" %c",&indicator);


}while((indicator == 'y') || (indicator == 'Y'));

// WHAT DO I DO NOW? ANY IDEAS?

getch();

return 0;

}

Please select your code and click on "#" to wrap it in code tags for the forum - always!

Your first number is both the minimum and the maximum, so far, so assign those variables to it:

max = tempnum1;
min = tempnum1;

Now use an if statement to test if any later numbers that are entered, are higher or lower:

if(tempnum2 > max)
   max = tempnum2;

if(tempnum2 < min)
   min …
Adak 419 Nearly a Posting Virtuoso

I believe I understand what the algo should be. You're using an insertion sort, like the last step in Quicksort, but you're using it over and over, on ever-increasing sizes of two groups.

So the algorithm is:

i = 0;
while(i++ < numbersToBeSorted/2)  {
   increase the numbers by one in both the first and last subsets
   and combine the subsets into one new subset.
   sort the new subset using insertion sort
}

//handle any odd middle number here, again by insertion sort

So you have three functions, one to make the left and right side sets up with the right amount of numbers, the second to combine them into one new subset, and the third to sort the new subset via insertion sort.

Here's a standard function for an insertion sort:
 void insertSort(int a[], size_t length) {
     int i, j, value;
 
     for(i = 1; i < length; i++) {
         value = a[i];
         for (j = i - 1; j >= 0 && a[j] > value; j--) {
             a[j + 1] = a[j];
         }
         a[j+1] = value;
     }
 }

//So let's use that for the sorting. Do not alter it, btw. Nothing breaks a 
//sorting routine faster than tinkering with it. :)

//One function down, two to go.

//print the a[] array, as it is, now

left[numbersToBeSorted/2 + 1] = {0};   //holds left side of our numbers array + 1
right[numbersToBeSorted/2] = {0};      //  "     right  "    "   "         "         "
i = 0;
while(i++ < numbersToBeSorted/2)  {

   //increase the numbers …
Adak 419 Nearly a Posting Virtuoso

I believe I understand what the algo should be. You're using an insertion sort, like the last step in Quicksort, but you're using it over and over, on ever-increasing sizes of two groups.

So the algorithm is:

i = 0;
while(i++ < numbersToBeSorted/2)  {
   increase the numbers by one in both the first and last subsets
   and combine the subsets into one new subset.
   sort the new subset using insertion sort
}

//handle any odd middle number here, again by insertion sort

So you have three functions, one to make the left and right side sets up with the right amount of numbers, the second to combine them into one new subset, and the third to sort the new subset via insertion sort.

You can't just compare the right and left subsets, as I understand your description. As the amount of numbers to be sorted increases, it will not work.

Adak 419 Nearly a Posting Virtuoso

Shouldn't your program have an int main() function, too?

:)

Adak 419 Nearly a Posting Virtuoso

I'll agree with you that you need help - anyone who names their array "element[]", is utterly at their limit! :)

I don't agree with your algorithm, however. It looks like it will fail when you are sorting more numbers which are not favorably arranged already, but I haven't run it yet, either.

I'll take a peek at it later tonight or tomorrow. Absolutely have no expectations about this, however. Sorting has been studied to death, btw. Is there some name for this kind of sort? May have info on the net by the bucket load, if so.

Adak 419 Nearly a Posting Virtuoso

<deleted>

Adak 419 Nearly a Posting Virtuoso

As I understand it, Binary search will always be faster if access times are constant (as in an array in RAM or cache), and Fibonacci search will be faster when the distance and access times, are proportional. Like on a hard drive or tape drive, where if the next item to be accessed is closer, the time to access it is much faster.

Adak 419 Nearly a Posting Virtuoso

I'm not familiar with C++, but a few points on prime numbers:

If no divisor is found w/o remainder less than or equal to the value of the square root of the number, then the number is prime. This really speeds up your testing.

Numbers which divide evenly by 2,3,5, or 7, should not even be tested as a divisor.

Just glancing over the code for finding prime numbers, it doesn't look right. Have you compared it's output with a reference list of prime numbers?

Adak 419 Nearly a Posting Virtuoso

There are no variables d or f. Those are just mistyped scanf "%d and %f", that were carried over to the variable name part of the scanf().

The program was compiling and working, with small numbers, so you know it was just an error in keyboarding it in, and what the error must be.

Adak 419 Nearly a Posting Virtuoso

Hi!

I am new to C programming, so this question might sound foolish. I have a problem with output of a program (sometimes the output is wrong)

Following is the program: (I am using Turbo C compiler)

/* Calculate simple interest */

#include <stdio.h>
main()
{
int p, n;
float r, si;
printf("Enter values of p, n, and r");
scanf("%d, %d, %f", &d, &d, &f); //should be &p, &n, &r
si=p*n*r/100;
printf("Simple interest=%f", si);
getchar();
return 0;
}

There is no error in this program. Output is also right but only for small integers.

e.g. If I enter p=1000, n=5, r=10, answer is RIGHT.

BUT for p=10000, n=5, r=10, answer is wrong (in minus value and totally wrong)

I think this is because of integer value limitations.

If yes, what is the solution to it so that I can use a bigger number.

If no, what is the reason for the error?

Please help me.

Thank you very much.

Yes, you've exceeded the limit for integers. Use unsigned longs, instead of integers.

That will solve your problem until you need *much* bigger numbers. :)

Adak 419 Nearly a Posting Virtuoso

If you want to use a pointer, then use a pointer, not the indices.

Read up on the -> operator, and you'll be off and running.

Adak 419 Nearly a Posting Virtuoso

I have problems with searching & sorting strings in a file...
I want to alphabetize the string so I did this

{for(x=0; x<100; x++)
	     {
 		for(c=x; c<15; c++)
	        fscanf(stdin, "%s", student[c]->surname);
	        if(strcmp(student[c]->surname,student[c+1]->surname)<0)
		  {strcpy(temp,student[c]->surname);
		   strcpy(student[c]->surname,student[c+1]->surname);
		   strcpy(student[c+1]->surname,temp);
		   strcpy(temp,student[c]->name);
		   strcpy(student[c]->name,student[c+1]->name);
		   strcpy(student[c+1]->name,temp);
		   strcpy(temp,student[c]->number);
		   strcpy(student[c]->number,student[c+1]->number);
		   strcpy(student[c+1]->number,temp);}
	      }
	 }

when i run the program, it just hangs. but i couldn't think of other ways on how to alphabetize it, other than comparing two strings then swapping. is there something wrong with my code above?

I also can't search properly...

printf("\nEnter first letter of surname to search\n");
		gets(search);
		 fscanf(r, "%s", student[c]->surname);
		 if(!strncmp(search, student[c]->surname,1))
		  {printf("\n%s, %s\t%s\n", student[c]->surname, student[c]->name, student[c]->number); break;}

nothing shows up when i enter a letter to search.. it just skips the above code and executes the next step (I didn't pasted it above, it's the printf("\nSearch again\n"); )

I'm really stumped on what to do. Any ideas?

You can't sort because you don't have a sorting algorithm - just some code you threw up that felt right at the time, imo. You need to use standard algorithms for sorting. Sorting algorithm's are typically "brittle" -- you change them a bit, and they "break" right away.

I'm just guessing that your gets(search), is just getting a newline still in the input buffer from a previous scanf(), perhaps. That's why it "skips" the code it seems.

put a getchar() ahead of the gets(), and see if that helps. (gets() itself is very unsafe due to buffer overruns. ASAP, learn and use fgets(), using stdin as the …

Adak 419 Nearly a Posting Virtuoso

Looks like the variable "num" is not returned to main, the variable "n" is never passed the value of num.

That leaves your sorting function with 0 strings that are being asked to sort. Return num and assign it to n, and see if that doesn't help.

Adak 419 Nearly a Posting Virtuoso

plz provide the c program to remove the blank spaces

No.

That's not what we do. The way it works is *you* post your code, and also post up a description of your problem or error with that code.

We will take a look at it and suggest ways of fixing it.

We don't *do* your homework, we help you with *your* code problems.

Adak 419 Nearly a Posting Virtuoso

Imagine you're in a large house, with several rooms. Each room is a function:

*you can only carry in *copies* of what you want, into each room, unless it's already been put there (ie, it's global in scope).

*you can only walk out of a room, carrying *one* item

*if the item you walk out with was created within the room, then the item will self-destruct right at the doorway as you walk out out of the room

*each room is organized so you can more easily do some type of work, in it. It has a natural purpose (sorting, searching, reading data, writing data, editing data, getting input from the user, etc.)

Hope that helps.

What you want to do is post up some code (the smaller and simpler the better), that you don't understand, or that doesn't work correctly, and ask specific questions about what you don't understand about that code. General questions like this, are too vague, and really require a chapter in a book, to answer thoroughly.

jephthah commented: that's kinda cool. never heard anyone put it like that before. +4