Adak 419 Nearly a Posting Virtuoso

Best explanation for it is on Wikipedia. Bookmark Wikipedia if you haven't done it already. They have a huge amount of programming pages, and a portal just for algorithms, IIRC.

Adak 419 Nearly a Posting Virtuoso

The getstr function is dodgy. scanf() is looking to take in just one char, but the user is trying to enter a string. And scanf() has to have an enter key to assign a value (buffered input).

fgets() is the function that immediately comes to mind for string entry. Overflowing the buffer array is no problem, and only one enter key needs to be pressed for each line. Also, the enter key is removed from the keyboard buffer with fgets(), and the end of string marker char is automatically added for you, if you just leave a space for it in the buffer.

fgets() for the win, imo.

Post back if you need help with fgets().

Adak 419 Nearly a Posting Virtuoso

ya...i can do...but it requires a file to store the user data...

Welcome to the forum, Snivas!

As for the file being needed - <wink> <wink> I KNOW you were joking.

Please God, that's all I ask for today, let Snivas be enjoying zee little joke. ;)

Adak 419 Nearly a Posting Virtuoso

The factorial function doesn't have a prototype in the program. My compiler wouldn't complain about this, since factorial() is above main(), but strictly speaking, it's bad form.

Does your compiler want another include file added to support long long int's?

Adak 419 Nearly a Posting Virtuoso

It all depends on how fast you need to develop the program/robot, and how fast you need the program/robot, to respond or to perform.

Python would undoubtedly be easier to develop than C or C++, but you'll never get the responsiveness in Python or Java, that you would get in C/C++.

If I'm programming a car to drive itself in a race, or a fighting bot, then I'm using C/C++ - period. If it's for a 2 mph robot that traverses a maze, then Python/Java/C# would be my choice. Very fast action/reaction time, just isn't needed for a slow mover.

Adak 419 Nearly a Posting Virtuoso

I don't see any reason for the ab or s variables. You AND the char from the mystery file, with the n that is generated (or assigned as 1 on the first loop), and that's it.

You can see that s especially, is never used in your loop. n remains unchanged by

s= ab & n;

Now, two questions:

1) What happened to using code tags?
2) When will cartoon characters receive orthodontic care for a bad overbite? ;)

Adak 419 Nearly a Posting Virtuoso

Did you initialize n to one? It's not in your code.

Also missing is the closing curly brace at the end of the for loop.

The for() loop CAN work, but it has to be done right. Also, the while() loop is much more flexible.

And PLEASE use [code] tags around your code, when you post it.

Adak 419 Nearly a Posting Virtuoso

Really, I want you keep that switch statement - keep working with it. Then you'll wind up pulling your hair out in frustration, and I'll look a lot better than you, in our forum yearbook pictures, because you'll be nearly bald. ;) ;)

Adak 419 Nearly a Posting Virtuoso

Print out some of these examples, and see if it begins to gel for you. Work with it, a bit. We learn best by doing.

Adak 419 Nearly a Posting Virtuoso

You're welcome. Carry on! ;)

Adak 419 Nearly a Posting Virtuoso

What is your question or problem with this code?

Adak 419 Nearly a Posting Virtuoso

That's not the code I posted for you. If you want to use code that doesn't work, then I can't help you.

Adak 419 Nearly a Posting Virtuoso

Use an if() with else if() series of statements.

When Narue speaks, wise coder's listen. Get those ears cleaned out, dude. ;)

Adak 419 Nearly a Posting Virtuoso

That code works fine. Did you paste it in, or just type it?

Did you get the change from <= to just < in the for loop?

Any other problem, you'll have to post your whole updated program, because I tested the above, and it works with your previous code.

Are you getting an error about:
"function should return a value in main()" ?

void main() is not permitted in standard C. Only int main() with a return 0 (if normal), at the end of the function.

Adak 419 Nearly a Posting Virtuoso

So if corect=0 (false), then what does !(false) evaluate to?

Hint: not false, ;)

Adak 419 Nearly a Posting Virtuoso

int width = 7;

Then replace your blank printing loop with:

for(k=width;k>(i*2);k--)
                {
                               printf(" ");                     
                }

Note that the loop above prints just ONE space char, at a time, instead of two space chars, as the current loop you have, does.

Adak 419 Nearly a Posting Virtuoso

if() and while() and for() statements all have a test. The answer to that test will be either zero or non-zero.

A zero answer means the test result evaluated to false. A non-zero answer means the test result evaluated to true.

if(0 < 1) {
  printf("\n Zero is less than one");  //statement was true
else
  printf("\n Funky arithmetic going on here!"); statement was false
}

With while() statements, the test is right at the top of the loop - which means the loop may never loop at all, if the test evaluates to false.

while(1 < 0)   //a false statement
  printf("This is impossible!"); //will never get printed

The ! char is the NOT operator (aka negation). Which "flips" the evaluation around.

Take the above, and make sure you understand why they do what they do, in your compiler. Then add the ! in front of them, and see if you understand why it's doing things differently.

Keep in mind that !(true) equals false, and a negative number as the final result, is just as true as a positive number. Only zero evaluates to false.

Adak 419 Nearly a Posting Virtuoso

You have the mystery.img file in the same directory as your program? You're using the display.exe to view the output file, after you've decoded it?

Post your code, and I'll tell you what's up with it. I'm not going to post the solution code, until you've put some real work into this assignment.

Don't be lazy, this is a good assignment, I promise.

Adak 419 Nearly a Posting Virtuoso

Watch your dates, Acer - this thread was from 2004. <Yikes!> ;)

And Welcome to the forum, Acer!

Adak 419 Nearly a Posting Virtuoso

Because the percent char is used internally, by the printf() function, as a part of a format specifier.

In essence, you're telling printf() to print out a value or variable, with the %! format protocol.

Which fails, because there is no %! format protocol, so printf() just prints out the exclamation point normally. It "eats" the % since it thought it was a format specifier, and it always "eats" format specifiers (replacing them with actual data in a specific format).

If you study your header files (stdio.h in this case), you might learn a lot, but the truth is, when you get far enough "under the hood" on these "Why?" questions, you'll wind up with one answer, before long:

"Because it was made that way".

C was made up by just a handful of programmers, based somewhat on the B language, which was based somewhat on the A language.

If you're going to have format specifiers for your print function, you have to have chars dedicated to those purposes. And you don't want those char's being printed, themselves.

So there you are.

Adak 419 Nearly a Posting Virtuoso

I mis-read your post, so let's clarify:

printf("15%!");   //prints 15%!
printf("15%%!");  //prints 15%! same as the first one

If you want two % char's you need:

printf("15%%%!"); //prints 15%%!
Adak 419 Nearly a Posting Virtuoso

That was just a program outline, not to worry. The actual code does these checks.

I recommend this exercise for you Abhimanipal - it's quite interesting, imo.

Adak 419 Nearly a Posting Virtuoso

Think of %l as just ONE thing - a format specifying the data type to be printed. So what's left is just one % char to print.

Adak 419 Nearly a Posting Virtuoso

The loop logic is pretty simple:

n=1
while(there is another byte to be read from the encrypted image file) {
  get that char from the file
  char = char XOR n  //use the ^ symbol for XOR
  write the char to the output file
  use the number generator equation to get the next n
}

You can use getc() and putc(), or fread() and fwrite(). Since you know the exact size of the file in bytes, you could use a for loop, but a while loop is much more flexible and robust.

And when you open the new output file with the display.exe program posted (link posted above), there's the image - buck teeth and all. ;)

* When * will cartoon characters demand good orthodontic care?? ;)

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Pdoratis! ;)

The thing is, you NEED some experience with C, to work on a C program - that's the simple fact.

We won't just do your assignment for you - especially important is for YOU to put some effort into the start of the job. You know how it goes, a lot of people come here, just looking for free homework programming, and have no intention of working on it.

You need to start the program, and post the program, even if it's not done. Post it. Tell us where you're stumped - with logic or syntax, whatever.

THEN we can be a lot of help, AND you'll learn a lot more than by just having it done for you, without putting any work into the program.

The program you want will take a good deal of time for a raw beginner, so don't put it off.

Adak 419 Nearly a Posting Virtuoso

I answered your rounding question already, but I'll repeat it:

If you have a ticket with a time of 1 hour and 1 minute, the assignment requires you "round" the hour figure up to the next number. So the above figure changes to 2 hours and 0 minutes.

You'll always have 0 minutes after you round up to the next higher hour.

The ONLY time you won't round up an hour, is if the time on the ticket, has a minute number of exactly zero.

Adak 419 Nearly a Posting Virtuoso

The assignment says to round hours only upward. Which means if you have ANY minutes at all, even 1, you need to increment their hours variable one time.

I like the way the program handles the Midnight problem - very nice.

I'd suggest you make your code more concise, however:

switch(num)
     {

       case 'c':




		printf("It'a car\n");
		display();
	 
		car();

		break;

       case 't':
...

should be:

switch(num)
{
   case 'c':
	printf("It'a car\n");
	display();
	car();
	break;

   case 't':
...

It makes it easier to understand your program, when we can fit more of it, onto one page. Overall, you have a good style, but again, I fail to believe you can't check your answers, by paper and pencil (maybe a calculator).

If you are going to program, you have to get used to testing your program for accuracy - that's the #1 priority.

Adak 419 Nearly a Posting Virtuoso

Other way around, Daredevil. ++*ptr will increment the value that *ptr points to, not the pointer, itself.

And *ptr++ is an increment to the ptr address.

so *ptr++ is *(ptr++) incrementing the ptr address
and ++*ptr means ++(*ptr) and increments the value that *ptr points to.

How does that compare to what you're getting?

Adak 419 Nearly a Posting Virtuoso

Got 'em. You got a PM also.

Interesting. TTYL.

Adak 419 Nearly a Posting Virtuoso

I don't have any XOR code for this, atm, but it's not hard to do that part of it. I might need to tinker with the congruential generator part of it.

Do you have any info on the file header? Should that be XOR'd also, or should only the data portion of the file be XOR'ed? If the file header is to be left alone, then I need to know how long the header is, in bytes, or it's end of header marker.

I would need a copy of the encoded image file so I could check it out with a hex editor for details, as needed. If you want help with that, load the image to Swoopshare, and PM me with the URL to the encrypted image file. Or just post the url in this thread.

Adak 419 Nearly a Posting Virtuoso

The program you posted does NOT decrypt anything - it just copies a file. If you need to reverse an exclusive OR, etc., then sure, you'll need more code.

;)

Adak 419 Nearly a Posting Virtuoso

First thing is to check that your program will copy a file without making any changes to it.

Take a jpg or other file and let your program copy it, - just copy not encrypt or decrypt it.

Then go to the console window (terminal), and type FC filename1 filename2, using the correct filenames, of course.

If Windows tells you "No differences encountered", then you know the file has been copied correctly.

You moved return 0 down to the last line of the program?

Adak 419 Nearly a Posting Virtuoso

That's good. My compiler liked the program, (no errors or warnings), but also ran out of memory.

Empty lines can be detected by:

if(strlen(buffer)< 2)
  //line is one char - undoubtedly a newline char: '\n'
  break; //or continue; are common
Adak 419 Nearly a Posting Virtuoso

For a beginning student, TC can work fine. There's no doubt TC is the easiest and the fastest IDE to set up, and get help in.

It works fine in XP and all earlier versions of Windows.

Naturally code blocks or visual express would be better choices, but they can't be installed and get the student working, as fast and easy.

Before you start your program, carefully think out what you want the program to do, step by step. Write out some pseudo code. Try to design it, more than stumble upon something that might suffice.

Worst thing to do is the obvious - sit down at the computer and start coding up "something" right away for your program.

We will help you with the C language part of it (although you need to show work on this part, as well). Before that however, YOU need to decide what you want your program to do - there are hundreds of ways to make a typing tutor program, work. Only you can decide on the design you want.

Adak 419 Nearly a Posting Virtuoso

One obvious problem is that the program closes the files AFTER the program has ended <so it's not closing the files, at all>.

Your IDE MAY close the files for you, but that's your IDE, not your program.

I don't see anything wrong with the rest of the program.

Exclusive OR is like a light switch - one way and the data should be encrypted, do it again, and the data should be unencrypted, IIRC.

Yep: Google Exclusive XOR

Adak 419 Nearly a Posting Virtuoso

You should return the index to the number, not the number. Then use the index, in main(), to print out the number.

Free the array whenever you no longer need it.

And you don't need to cast the return from malloc, in C.

Adak 419 Nearly a Posting Virtuoso

That looks very good!

This is one of the finest examples of Quicksort I've seen. Tested fast, clear, and easy to optimize further, if you wish.

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

  if(lo == hi) return; 
  i=lo; 
  j=hi;
  pivot= A[(lo+hi)/2]; //change to (lo +(hi-lo)}/2 and re-test

  /* Split the array into two parts */
  do {    
    while (A[i] < pivot) i++; 
    while (A[j] > pivot) j--;
    if (i<=j) {
      //swap(A, i, j);   //this line works fine.
      /* Oddly, on large N, using swap() gives the same time, as
         using the swap code below */
        temp= A[i];
        A[i]= A[j];
        A[j]=temp;
      i++;
      j--;
    }
  } while (i<=j);
    
  if (lo < j) quicksort(A, lo, j);
  if (i < hi) quicksort(A, i, hi);
}

Enjoy!

Adak 419 Nearly a Posting Virtuoso

If the compiler's error or warning says the function should return a value, then I'd try and have the function return a value. ;)

Prototype your functions, and make sure the actual function return types, match the return types of your prototypes.

Adak 419 Nearly a Posting Virtuoso

I surely don't like it.

I can see having two returns in a function. One is preferred, but two is OK. Four returns in one function, is not OK.

mid should be set in ONE line of code, outside of the if else statements instead of being inside, in two different places.

if(n < a[mid]) should set max to mid-1, instead of mid. n is, after all *less than*, a[mid].

if(length <2) is unnecessary.

Take a peek at one of the good binary search functions, and compare it, with yours. This forum has some good examples.

Adak 419 Nearly a Posting Virtuoso

His question referred to ++*ptr, not *++ptr.

Adak 419 Nearly a Posting Virtuoso

I can't tell you, but I can tell you how to troubleshoot it:

1) Copy down the EXACT error message, for reference
2) comment out the calls to functions, one by one, until you find out which function is causing the error.

3) going into that function - comment out various lines, one by one, until you figure out exactly what line of code is giving you the error.

4) Check that out as thoroughly as you can

5) Still stuck, post THAT info back here, and we'll be able to help you more.

The troubleshooting described above is a basic programming skill, which you will have to learn - and learn well - if you're going to program.

"one by one" doesn't literally mean just one function or line of code at a time. You might do several at a time, and keep cutting the amount you're commenting out, down by half, each time you re-try.

Similar to a binary search algorithm. Same goal though - find the faulty line of code.

Adak 419 Nearly a Posting Virtuoso

No, I can't tell you, because on my compiler, if:

int n=3;
int *ptr;
ptr = &n;
++*ptr;
printf("\n%d", *ptr);

prints out: 4

So ++*ptr is the same as ++(*ptr).

*ptr++, on the other hand, is the same as *(++ptr) - meaning it does increment the pointer address on my compiler, so the output is usually 0, but conceptually, could be any integer.

When you have an expression to be evaluated, and you're not sure of the precedence for it, explicitly set it up with the right parenthesis's.

Adak 419 Nearly a Posting Virtuoso

Use while((fgets(bufferName, sizeof(bufferName), filePointerName)) != NULL) {

instead.

Testing for the end of the file has become quite unpopular (and deservedly so), for just this reason (among others). Don't use it.

Adak 419 Nearly a Posting Virtuoso

Sorry, you need to show some work on this. Just posting your assignment and waiting around for some shmuck to do your homework for you, doesn't meet the requirements of the board.

I find it impossible to believe that you can't figure out how to calculate the charges and time, etc.

Adak 419 Nearly a Posting Virtuoso

I refuse to answer, on the grounds that you should be enthusiastically TESTING this yourself. ;)

Put down your latte, take 30 seconds, and test it, FCOLoud!

Adak 419 Nearly a Posting Virtuoso

This is not a polished program, consider it a starting point. The "penguin boys" can give you a hand with the shell commands that it will need, for Linux. (They probably can be done in C, but not on either of my compilers [which are for Windows and DOS respectively]).

I used Turbo C for this, BUT didn't use any TC specific extensions, that you couldn't use in Linux. For that reason, this program requires all the filenames to be DOS compatible (8.3) maximum length. Your version on Linux, will have no such trouble, so don't worry about that.

These are the five files I worked with:
10_03_03.MSR
10_03_04.MSR
10_03_05.MSR
10_03_06.MSR
10_03_07.MSR
which you provided, after being shortened. Just tested it with the longer filenames, in the shell, and Windows XP handles it OK, using some behind the curtain work with re-naming of it's own. So these filenames:

Reg_10_03_03.MSR
Reg_10_03_04.MSR
Reg_10_03_05.MSR
Reg_10_03_06.MSR
Reg_10_03_07.MSR
all worked, although they're longer than 8.3 limit.


They were located in a known path and filename. This program can be run from anywhere it can access the command shell.

This is the output from the five files:
2010-03-03 23.231148 86.688525
2010-03-04 22.540984 88.180328
2010-03-05 20.672131 88.688525
2010-03-06 20.670492 89.672131
2010-03-07 15.526230 88.475410

The first field is the date from the first row of the file, the next two fields are the averages …

Adak 419 Nearly a Posting Virtuoso

The indentation is (sadly) missing, but I also liked this pseudo code, a lot:

http://www.daniweb.com/forums/post22184.html#post22184

Without indentation to show dominant and subordinate lines of code, I think it fails, but otherwise, it's great.

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Anzoo! ;)

Here's the way to use the forum - give your assignment a good try, and then post your try. Tell us what has you stumped.

If we don't do that, we become "homework coders" for everyone (or so it seems), and the students can't learn much that way, either.

You'll need to create an array of integers with two numbers on each row. Set the array values to correspond to 1) the weight and 2) the shipping charge, on each row.

Then you can scan the array with a

while (package weight < shipping weight number[i++]);

1 line while loop. Sweet! :)

and carry on from there.

Adak 419 Nearly a Posting Virtuoso

Of course, I'm on Windows, and wanted to try and do this with Turbo C, which is a mess, because it wants to let me get just old DOS 8.3 file names, instead of the long one's.

So I've been having quite a fun of fun trying to work around that. The funny answer is to have TC call a bat file, then the bat file calls the right cmd.exe (command file), and THEN I can get my long file names (after boldly leaping through a few more hoops). Ah me! ;) ;)

You know until 2 weeks ago, I had a Ubuntu system up and running - that's just weird! (Ubuntu is based on Debian)

Anyway, I have a version that now opens the second or inner file, and reads each line, in that file. I'll finish roughing it out, tomorrow.

I'm putting off the details for now, until the big stuff is up and working OK.

On the averages - if our data is:

field #3 are: and field #6 is:

50.00             8.0
51.00             8.2  
49.00             7.8

Would our output averages be:

50.00             8.0

or are you averaging field #3 together with field #6?

If it's the latter, you're going to have to give me an example. I wasn't expecting that kind of a merge of field data.

Given the above numbers (simplified I know), what would the correct output be if they're merging together, somehow?

TTYL

Adak 419 Nearly a Posting Virtuoso

continue statement is designed for loops (for or while, or do while). Wouldn't make sense to try and use it in an if/else statement.

You can have a "do nothing" in an if statement, however:

if(numberOfApples > 1)
  ;               //nothing gets done
else
  printf("\nNo apples here yet!");

Good pseudo code is just structured English or language:

for each apple in the bowl {
  if the apple is ripe
    remove the core
    remove the skin
    slice it into 8th's
    roll it in cinnamon and sugar
    put it into the pie
  else
    if the apple is green    
      return it to the bowl
    else
      throw it out
}

Mine is generally a toss up between BASIC, C and Python. The key thing is that the logic has to be understandable. I'm no pseudocode guru, however. ;) I'm sure your teacher will have a preferred way that he/she wants it done. THAT's the way to do it, as least for now. :D