Adak 419 Nearly a Posting Virtuoso

Just use a different name - con is taken by the system. Doesn't matter how you try to get the program to use that name.

System has first "dibs" on it. ;)

Adak 419 Nearly a Posting Virtuoso

The more your program does, and the more efficiently it does it, the higher the percentage of cpu utilitization will be.

That's not a bad thing. Sometimes just a few lines of code will generate 100's of lines of assembly or machine code.

If your program is starting to have longer run-times, and still has high cpu utilitization, then you have a worry to be checked out.

Adak 419 Nearly a Posting Virtuoso

Hi Nakul,

Try to learn C the right way:

1) It's always int main(), never just main() or void main(), and it always has a return integer (generally always 0).

2) now let's make your program look like a *program* on the forum:

Just click on the [.code] icon, in the advanced edit window (I added a period so it would show what it looks like).

Note: code tags, int main(), return 0, a getchar to hold the console window from closing too fast, AND good 3 space indentation. All add up to a better program and programmer.

The indentation allows programmers to use their experienced eye to spot trouble, much more quickly. You will be doing that, as well, before long.

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

int main()
{
   int x;
   for (x = 1; x <= 250; x ++)
      printf("x = %d\n", x);

   printf("\n\t\t\t     press enter when ready");
   getchar();  //holds the console window open, if needed
   return 0;
}

Try this version of your program, and see if the problem still exists.

Adak 419 Nearly a Posting Virtuoso

For a percent of hits, the equation is:

number of hits / number of guesses

not divided by number of misses. Missing percentage would be:

100 - percent of hits = missing percentage.

And always times 100, if you want to express it that way.

So in your function, you need to only return one value - the percent of hits could be that value.

Instead of using a pointer (did you set that pointer to point to the base of the struct?), you could just assign the struct member, with the return from your function:

with a call like this:

yourStruct.percentHits = calculate_hits(int hitNum, int guessesNum);

And changing 
void calculate_hits(int int) to
float calculate_hits(int int)

Nothing wrong with using a pointer to a struct, but there's nothing wrong with using the return from a function - which is what you describe the function as doing, btw. ;)

Adak 419 Nearly a Posting Virtuoso

Or are you also implying that when using DevC I can't create a variable named clrscr or gotoxy because it's defined in TurboC3?

I was implying nothing of the sort. I said nothing about DevC, you, or any variable you can or can not create.

It was directed at Jay.

Adak 419 Nearly a Posting Virtuoso

WaltP: please don't put words into my posts that aren't there. I'm saying no such thing. My comments were intended for Jay.

Jay, I'd have to change a few things in your program, to make it work, would that be OK with you?

Adak 419 Nearly a Posting Virtuoso

It is? What's it used for? Please verify before replying.

[B]random[/B]   Macro that returns an integer.
 Syntax:
   random(num);
   int random(int num);

Defined in  stdlib.h

 Remarks:
random returns a random number between 0 and (num-1).

random(num) is a macro defined in stdlib.h. Both num and the random number
returned are integers.

 Return Value:
Returns an integer between 0 and (num-1).

 Portability:
A corresponding function exists in Turbo Pascal.

 See Also:
  rand    randomize    srand

 Example:
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>

 /* prints a random number in the range 0 to 99 */
 int main(void)
 {
    randomize();
    printf("Random number in the 0-99 range: %d\n", random (100));
    return 0;
 }

Not every compiler may have this, but some do.

Adak 419 Nearly a Posting Virtuoso

One problem I see is that you're using the word "random" as a variable. Change that name, because "random" is a reserved word in C.

If that doesn't fix it, post back and I'll run your program and see what else looks troublesome.

Adak 419 Nearly a Posting Virtuoso

You could do a search that would be faster, but there is absolutely no need for that, in this game. A sequential search is fine!

Some idea code - untested!

if(gameOn) 
  loop for another player's turn
else
  give congrats and goodbye message and exit this game
  in that function ask them if they want to play again

int gameOn( game array, int numRows, int numCols) {

for(r = 0, gameon = 0; r < numRows; r++) {
  for(c = 0; c < numCols; c++) {
    if(game[r][c] == value of opponents ship) {
       gameon = 1;  //gameon is just an int variable you will declare
       break;  //enemy still has an unsunk ship, so game on!
    }
  }
}

return gameon;

//your calling function should handle gameon == 1, or  gameon == 0
Adak 419 Nearly a Posting Virtuoso

You can't run Turbo C graphics on another compiler. I couldn't compile your program in Turbo C, since it was missing a parameter (noted on the line in question, below).

But Turbo C is pretty sweet for doing small assignments and programs. I use it a great deal.

The first program below, is yours, the second one is the help example from Turbo C, itself. Yes, it runs in WindowsXP.

/* This is your code. It will not compile, as is, in Turbo C.
#include<stdio.h>
#include"graphics.h"
#include<stdlib.h>
#include<conio.h>
void main()
{
     int graphdriver=DETECT,graphmode;
     int color,n,m;
     initgraph(&graphdriver,&graphmode);  //missing last paramenter!
     for(n=0;n<10;n++)
     {
                      putpixel(250+n,350,BLUE);
     }
     for(m=0;m<10;m++)
     {
                      int x=getpixel(250+m,350);
                      printf("%d",x);
     }
      getch();
*/
//This is the help file from Turbo C, which runs fine in WindowsXP, //with a default install of Turbo C.
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();

   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   }

   /* draw a line */
   line(0, 0, getmaxx(), getmaxy());

   /* clean up */
   getch();
   closegraph();
   return 0;
}

i

jephthah commented: no, turbo c is not sweet at all, for anything. it sucks and perpetuates suckiness everywhere its use is forced onto hapless students by incompetent administrators in sloppy Second World programming courses. -1
Adak 419 Nearly a Posting Virtuoso

If what he wants is a LINE editor, then he should follow the functionality you can see in Edlin. That was a terrific suggestion from Ancient Dragon.

As far as editing, I'd definitely want to use a compiler with conio.h or ncurses, which have that getch() feature. Also, the gotoxy(), is there as well (although Windows gives you SetConsoleCursorPosition(), which does the same, but it's a bit clumsier). Couple that with wherex(), and wherey(), and you're off to a good start, imo.

The multi-line editor is the better editor, no doubt. The Edlin type (one line at a time) editor is the much easier one to program, however.

So he needs to decide what he wants.

Adak 419 Nearly a Posting Virtuoso

Wow! Edlin takes me back. Used it in my first computer language, BASIC.

Now I find it hiding in WindowsXP, and still working!!

Adak 419 Nearly a Posting Virtuoso

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

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

#include <stdio.h>

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

int main()

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

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

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

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

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

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

      }
    }
  }
  printArray(array1);

     // system("PAUSE");

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

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

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

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

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

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

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

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

Adak 419 Nearly a Posting Virtuoso

Change the return from your compare function to:

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

And it works fine. Nice program, btw.

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

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

Adak 419 Nearly a Posting Virtuoso

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

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

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

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

Adak 419 Nearly a Posting Virtuoso

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

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

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

:( 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

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

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

<deleted>

Adak 419 Nearly a Posting Virtuoso

May I suggest you use the digits 1, 2 & 3, instead of 0, 1, & 2?

Because you can't "make" a number which begins with 0's: 01, 001, 02, 002, etc., are not a number. Anything beginning with a real number, would be fine though: 101, 20, 103, etc.

Other idea's would include using bit values within the bytes, but that sounds unnecessarily difficult for a beginner, and keeping the values in an integer array, where the zero's would be fine.

Always put your code between code tags on any forum.