Adak 419 Nearly a Posting Virtuoso

A slight improvement, but not a lot.

#define X scanf("%d",&
main(){int n,s=0,t;X t);for(;t-->0;){X n);s+=n;}printf("%d",s);}
//86 chars including five spaces, required.

The 5 spaces aren't counted against your program, according to the quiz site - putting this at 81 chars long.

Adak 419 Nearly a Posting Virtuoso

I'll try, but I have to warn you, I don't practice this kind of odd programming, and am not sure I can do anything better than what you have done already.

Adak 419 Nearly a Posting Virtuoso

I wasn't too clear on that.

You don't want to use an if statement to process only odd numbers - that's still doing SOMETHING with even numbers. You don't want that.

What you want to do is, instead of simply incrementing the number to be tested next by one with number++, use number += 2. That means 2 and 3 have to be done before the final loop through the rest of the numbers.

Try that, you'll like it. ;)

Adak 419 Nearly a Posting Virtuoso

Since the number being tested is an integer, and the actual square root value (the floating point value), is meaningless in this context, using just the int value of sqrt(n) is fine - and smart.

However, you are now recalculating this value (the int square root of n), over and over, in your for loop. You should calculate that value BEFORE the for loop begins, and then use it throughout the looping, to test against -- never recalculating it. It's the same value, and getting the square root is an expensive bit of code, requiring MANY cycles.

Once you change this, and change the checks so only odd numbers are tested, you'll see your run time cut by about 2/3rds! ;)

I like your overall program design - very smart.

Adak 419 Nearly a Posting Virtuoso

I don't have time to work with this right now, but I was wondering if a very short recursive function could help?

Adak 419 Nearly a Posting Virtuoso

A far better choice: Pelles C - free (only for Windows), but 32 or 64 bit, and unlike Turbo C (which I dearly loved btw), it's up to date, including having it's own support forum.

Like Turbo C, it's intuitive to use, and has plenty of help. You'll love it, AND be WAY more up to date and able to program in Windows, etc.

Ancient Dragon commented: thanks for the link :) +36
Adak 419 Nearly a Posting Virtuoso

I ran them on Windows 7 and Windows XP OS's, but you do have to hunt around for them, and not all of them worked.

Adak 419 Nearly a Posting Virtuoso

You are using some confusing terminology. If you want to treat memory as if it was a disk, you might want to look into tmpfs or something like that. Or you could attempt to create your own fs "format". Are you supposed to write a driver? This seems like a hard project for "a beginner" like you stated before. Maybe you have misinterpreted what your teacher said.

You don't know what a RAM disk is obviously. ;)

A RAM disk is memory that is set up to emulate a hard drive. The system will recognize it as a drive, separate from your other memory, and you access it the same way as a drive.

It holds files, has sectors, etc.

A RAM disk has a few distinct advantages:

1) If your info is VERY sensitive, the instant the machine shuts down, the data is gone, and can't be recovered - quite a bit more secure than any normal hard drive.

2) Access times to the RAM disk is VERY fast - it is memory, after all, and has no mechanical parts that have to be moved. (read/write head, spinning platters)

In a recent project of mine, the program generated millions of files, every day. Each 1,000 file group of files, had to be quickly checked, and then could be over-written. So I used a RAM disk, just to speed things up, (the project took months to complete), and to save the wear and tear …

Adak 419 Nearly a Posting Virtuoso

Use a good sized char array[100] or so, and fgets(array, sizeof(array), filePointer), to put one row of data into the char array.

Now, in a for loop from array[0] to array[strlen(array)], count up your spaces and add 1 - that is how many numbers you have in that row.

now malloc() memory for that size row, and sscanf() your numbers into the numbers array (which is a 2D dynamic array, of course, of type int).

The numbers array would be "ragged" in length on the high side, but save the maximum amount of memory, if that's what you want or need.

Personally, I'd use Walt's idea, simply because memory is plentiful. BTW, you could do this same type of thing with nodes, in a linked list.

t_man700 commented: Thank you for your response. I appreciate it! +1
Adak 419 Nearly a Posting Virtuoso

Pretty simple to code this up, why not try?

while there are more letters in the string 
  take each letter and put it into a char words[50][50], incrementing j,
  unless it's a space or punctuation char. (use ctype.h and isalnum() ). 
  In that case, increment i and start a new word[i][j] at the next isalnum()
  char.
end of while

You know this is painful to not give out code! I always wanted to be a God. ;)

You wouldn't learn much that way, though. Doesn't take a God to figure that out.

Adak 419 Nearly a Posting Virtuoso

You have been given the same answers in two different C forums, I hope that's enough cross posting on this subject.

Adak 419 Nearly a Posting Virtuoso

The above code doesn't have any structs, which are required by the assignment. They don't have to be like I suggested, but the program must include them.

I agree with not using strtok(). You can check using words.word1, words.word2, and words.word3 with strstr(), for each line of text (in a loop, of course - and fgets() works very well like that). You might think of a better struct to use, also - but use one struct, at least.

Adak 419 Nearly a Posting Virtuoso

@shinsengumi:
One of the problems with an on-line forum is that you can't always tell what reply is meant for which poster.

Anything negative you read here, don't take it personally. Keep focused on the programming topic of the thread. Best course is simply the "water off a ducks back" philosophy, that S.O.S. mentioned above.

As far as Narue and Ancient Dragon, they both have excellent programming skill, (better than mine), and as your program comes together, you'll see that it uses a loop very much like the one that AD outlined in pseudo code for you.

I get the sense that you have a good deal of pride in yourself, and that's all OK, (clearly, if we aren't taking any pride in ourselves, who will?), but too much will hold you back from learning all that you could. It is the empty cup that will hold more fresh tea.

So, back to finding strings within a larger string. ;)

Where are you with it? You don't need to wait until it's working properly to post it, btw. If you're stuck on something, post up what it is, and tell what you're stuck on.

And let anything negative that you see, just roll right off your "oily feathers". Which is not always easy to do, I freely admit. ;)

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, udinnet! ;)

Tell us a little about this program. Is it meant to find one solution, or all the solutions?

What kind of run times are you getting for 8 queens, on an 8 X 8 board? On what kind of system (cpu speed)?

Do you think it's slow, fast, or in-between? What is your opinion of it?

Adak 419 Nearly a Posting Virtuoso

After 15 posts, you should know to ALWAYS use code tags around your code.

I'm always a bit amazed by posts like this. You have your program, why not test it? It's VERY difficult to look at code and see if it's got a bug or not in it. An obvious flaw - sure, but lots of times there will be errors that only a test run will catch.

Adak 419 Nearly a Posting Virtuoso

Unfortunately, your post above, is all about your assignment - and not about using C style C++. This is the part of the assignment that you need to work through. It has so many parts to it, with so many constraints, that it's very unlikely anyone will be willing to donate the time necessary to fully understand it. I've read your description of the problem through twice, and have an insufficient grasp of the total problem yet.

And you can't even start to put up pseudo code, until you really understand the problem fully. :(

Use your class notes, your memory from the lectures, and if you're stuck, ask your teacher a class mate, or assistant, to explain some details for you.

My advice is to work it through until and unless, you have a specific problem that is C related, and not assignment related - then we can help you. If you can't describe the C problem in a paragraph or two, you need to get to the point that you can do that.

Adak 419 Nearly a Posting Virtuoso

Include string.h if you haven't already, then use strstr() which will return a pointer to the first letter of your sub string.

Now using that address, you can use memset() or memmove() to change the contents of the larger char array, to some sentinel value, blank spaces, or whatever you like.

Those are the two functions that will do the job for you. You will have to work with a char pointer or two, because that's what strstr() returns, not an index number. It's also what memmove() needs.

As mentioned above, you will need a loop, with the above.

Adak 419 Nearly a Posting Virtuoso

In C, you read a text file line by line, using

fgets(charArrayName, sizeof(charArrayName), filePointerName);

Which will automatically insert the whole line of text, into your char array, including the newline (and the end of string char: '\0', after that. (If room allows).

while((fgets(charArrayName, sizeof(charArrayName), filePointerName)) != NULL) {
  //your code here

}

Is a standard idiom in C to read every row of text in the file.

Railroad theme, I see. ;)

codeyy commented: very helpful +2
Adak 419 Nearly a Posting Virtuoso

I don't like that code for this assignment. There are no structs, and there are no functions. It might work, but it's not what you need.


how about having a prototype of a struct with 3 words:

#include <stdio.h>
#include <string.h>  //for strstr()
#define SIZE 100

typedef struct {
  char word1[]={"AND"};
  char word2[]={"OR"};
  char word3[]={"NOT"};
}words;

void check(char buffer[]);
 
int main(void) {
  //declare your variables for main, including a char buffer[SIZE]
  //use fgets(buffer, sizeof(buffer), fp) to get each row of text,
  //where fp is the FILE *fp for input, and the file has been opened in   
  //read mode.
  
  //call check(buffer); with each row of text
   //and make the changes to the buffer array
   //when it's OK, and returns from check, write buffer to the output file
   //using fp2, another file pointer, but for output.
  return 0;  
void check(char buffer) {
  //use strstr(), so you need a pointer to char: *pch
  //and you need one words struct like
  //words word, then use the dot operator: word.word1, word.word2, etc.
}

That should get you started in the right direction. Give that a try.

thekashyap commented: Compile the code you posted.. -1
Adak 419 Nearly a Posting Virtuoso

"As am I!", as the priceline commercial says, as am I. ;)

You haven't debugged it until you can sum up one or more columns, with one or more students (rows), and get the correct answers.

If you want to "pull" (another priceline moment?), the sum out of the function, then you need to send the address of sum, from the calling function, into your called function:

calling function(add &sum to the parameter list), called function(add double *sum to the parameter list).

Then within the called function, use *sum, instead of sum, because sum is just a pointer now.

Adak 419 Nearly a Posting Virtuoso

OK, well let's troubleshoot this:

1) Do you have the array values that you think you have? Print out the array, lined up by columns, and see if it's all OK.

2) As you step through the loop with a bad number of columns, what is going on when you watch the values in the array, and in sum?

Clearly, something fishy should be spotted! ;)

This is common, btw. So often, we think our code is just GOLDEN, and find out "oops!", it really hasn't been tested, until it's REALLY been tested! ;)

Adak 419 Nearly a Posting Virtuoso

I applaud your perseverance, but that is not an acceptable way to program. Use arrays and loops, especially for and while loops, to cut your program down to 1/10th that size and number of separate variables.

Adak 419 Nearly a Posting Virtuoso

Normally, you would sum up via row like this:

for(row=0;row<MaxRows;row++) {
  for(col=0,sum=0;;col<MaxCols;col++) {
    sum+=Array[row][col];
  }
  printf("\nThis row sums up to %d", sum);
}

//Now you just "turn it sideways", to sum by column:
for(col=0;col<MaxCols;col++) {
  for(row=0,sum=0;;row<MaxRows;row++) {
    sum+=Array[row][col];
  }
  printf("\nThis col sums up to %d", sum);
}

Give that a shot and see if it makes sense. ;) I used int's out of habit, only. Same pattern when you sum up columns of doubles or floats.

Adak 419 Nearly a Posting Virtuoso

fflush(stdin) is like trying to flush your kitchen sink.

Flushing only works on output streams (like the toilet in your home), not on input streams, like your kitchen faucet. ;)

Adak 419 Nearly a Posting Virtuoso

The way redirection work on Windows is simple:

If you have a command line like this:

programName <fileName

Then the standard input (keyboard generally), will be replaced by a stream input from the fileName.

Obviously, if you don't have a file named "fileName", in the same directory as your program, then your file won't be found, and it will quit with an error (as your example shows).

In this program, for instance, you can run it two ways:
With redirection, you need to give it a valid file name.

Without redirection, you need to enter a line of text, and hit enter. Without redirection, you quit entering text (and end the program), by hitting Ctrl+Z, which is the end of file, in Windows and DOS.

Try running this program, and see how it works:

#include <stdio.h>

int main() {
  char ch;
  
  while((ch=getc(stdin)) != EOF) /*read data file*/
    putc(ch, stdout);
  return 0;
}
Adak 419 Nearly a Posting Virtuoso

In data? Yes! ;)

In data[j]? No! Only one char will fit into data[j]

Adak 419 Nearly a Posting Virtuoso

Because data[j] is just ONE element, big enough for ONE char.

The string goes into data (which designates the row with i).

Print up some data and see what the contents are, after you change it as above.

Adak 419 Nearly a Posting Virtuoso

You still have that data[j] stuff in there.

LOSE THE DANG [j]! ;)

Maybe not with this small a program, but try to come up with better names than two arrays named "buff" and "BUFF". Maybe buff1 and buff2? Capitals are normally used for macro's (see SIZE?), and globals (Globalvariable, (also typedef's, if needed).

Adak 419 Nearly a Posting Virtuoso

Edit: this seemed to work OK, in my very limited testing.

fp is the file pointer, ch is a char, and the other variables are int's. ctype.h was included, of course.

while((ch=getc(fp)) != EOF) {
    putchar(ch);
    if((isalpha(ch)) && (inword==0)) {
      inword=1;
      wordcount++;
    }
    else if(!isalpha(ch)) { //isalpha(ch)==0
      inword=0;
    }
  }
  fclose(fp);
  printf("\n I counted %d words in that file\n", wordcount);

Give this a try, and if no luck, post back, but I think you'll get it from this.

Adak 419 Nearly a Posting Virtuoso

Not yet - that's the same logic as before, using different variables. :(

in the header file ctype.h, we have functions to test what kind of char it is. I'd use two of them, so include <ctype.h> at the top of your program.

Those two are ispunct() (is it a punctuation, and isalpha(), is it a regular alphanumberic char (letter or number)?

So the logic will be (approximately):

if((inword  equals 0) and (isalpha(char being checked))) { //
  inword = 1
  wordcount increments by one
}
/* handles last word in a row which has no space after it. */
else if(char equals ' ' or !isalpha(char being checked)) {
  inword = 0
}

May be more logic needed, but not much. It's been a long time since I coded one up. Give this a try, and post back up and I'll study it more.

Adak 419 Nearly a Posting Virtuoso

Instead of the simple if(char == space) logic, (which obviously can't handle multiple spaces), how about this.

inword = 0; changes to inword=1 when you find a word. When you leave a word, it goes back to 0.

First letter you run across, you set it to 1, and increment word counter. When you run into the first space, you set it to 0, and do not increment word count any more, until you run into the next letter.

You need to add some logic to handle things like the last word in a sentence, at the end of a file, etc.

Adak 419 Nearly a Posting Virtuoso

Yes, one problem is that data[] is just a ONE dimension array. When you put your string into it, then it's full, and that's all it can hold:

printf("data: %s\n",data[i]);

Makes no sense. data is one single char, not a string (%s), see what I mean?

If you want data[] to hold more strings, then you need to make it a 2D array:
data[][], where the first dimension is the rows, and the second dimension can be viewed as a column within the row. Then data could indeed hold an entire string.

I'm not sure of what you need, because I don't know what you want to do with the data, yet.

Adak 419 Nearly a Posting Virtuoso

I've never heard of "Flames". Since you've had no responses, I suspect others haven't either.

What should your output be, and what have you tried to fix it, so we don't have to repeat it?

Thanks for using code tags. ;)

Adak 419 Nearly a Posting Virtuoso

You're such a tease, Narue! ;)

http://www.cs.utsa.edu/~wagner/laws/AESDec.html

The link above, has C like code for EIC. The Green colored link at the top of the page, (to the book), allows a download of the entire book "Handbook of Applied Cryptology", in separate chapters. I haven't read the book yet, but it could help you with any background info you need on Cryptology.

So asking in sci.crypt on Usenet was helpful. ;)

Adak 419 Nearly a Posting Virtuoso

No. As stated previously, I looked through 8 pages of results on Google for this, and none of them had what you needed.

Did you try sci.crypt on Usenet yet? Have you tried taking the description of EIC, and coding it up from there?

That and more looking on the net, is all I can advise. Be persistent!

Adak 419 Nearly a Posting Virtuoso

Yes there is such a tool. "Valgrind". I'm unsure if it works without running the program you want to test, however.

Adak 419 Nearly a Posting Virtuoso

Viruses are a large, and constantly changing area of study. They may attack:

1) Through your browser (common), but any running program that can be "hijacked", including any part of the operating system, including the services, etc., will do nicely.

2) By tricking you into downloading a trojan, which is not seen as a virus. Later, that trojan will activate, and d/l the real viral code in turn, and activate it.

3) By replacing, deleting, or altering system files, or registry entries, and running a "guard" program to keep the victim from accessing help websites. Norton, Kaperski, AVG, Avast, MS, and others, become unavailable. Also, you may have no ability to see your Task Manager anymore.

4) By creating it's own rootkit. You now have a co-owner of your system, hello!

5) By monitoring your keystrokes (keylogging), and/or searching your drives for passwords, personal data, financial data, etc. Anything and everything of value that they can find, steal, and then use or sell.

Stay tuned for "updates with film", later! ;) Yes, it's the Wild Wild West as far as viruses go. Stopping them, without creating false positives (which would be worse than the virus), is a real challenge.

You will have to study viruses, and how they work, quite a bit, before you can write an effective program to defeat them. All of that, has to come first, before you can even think about writing any code for your program.

Adak 419 Nearly a Posting Virtuoso

I erred on the strlen(word)%2==0 idea. Not necessary, please drop it.

Here's your code, with a few tweaks. The tweaks are right below the code that needs tweaking:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<ctype.h>
#include<dos.h>


/*All these global variables, let's remove and make them local variables instead. Seems a PITA maybe, but it pays big dividends in programming.

char word[289];
char *str1,*str2;
char *rev;
char *upper;
char *lower;
char *dup;
char choice[3];
int a,b,x,y,i,h,z,j,temp;
*/

//void main()
int main(void) //<===== Always int main, never void
{
 int len, i, j;
 char dup[250]; //nice and big :)
 char word[250];  //  ditto
 char choice;
 //input:  //use functions and logic, and not labels 95% of the time.
  
 //clrscr(); //this crashes my IDE, immediately
 printf("\n\n");  //gives the separation we need
 printf("ENTER A WORD:");
 gets (word); //unsafe! but we'll use it for now
 printf("\nYOUR WORD IS:");
 puts (word);
 //dup=strdup(word);
 strcpy(dup, word);
 len = strlen(word);
 //menu:
 //for(z=1;z<=5;z++)
 do 
 {
   printf( "\n\n1:REVERSE\n2:ACSENDING ORDER \n3:DESCENDING ORDER\n\4:UPPERCASE HIGHER HALF\n5:UPPERCASE LOWER HALF\n:");
  gets (choice);
  printf("\nYOUR CHOICE:");
  puts(choice);

  switch(choice[y])
  {
    case'1': {
      rev= strrev(word);
      printf("\n\t\t\t\REVERSE WORD:%s",rev);
		 getch();
      // clrscr();
      printf("\nYOUR WORD IS:");
      puts (dup);
      break;
    }
    case '2':{
     for(a=0;a<strlen(word);a++)
     {
       for(b=0;b<strlen(word);b++)
       {
	  if(word[a]<word[b])
          {
	     temp=word[a];
	     word[a]=word[b];
	     word[b]=temp;
	     printf("\n\t\t\t\RESULT:");
	     puts(word);
	  }
        }
      }

      getch();
      //clrscr();
      printf("\nYOUR WORD IS:");
      puts (dup);
      break;
    }
    case'3':{
      for(a=0;a<strlen(word);a++)
      {
		  for(b=0;b<strlen(word);b++)
		{
		if(word[a]>word[b])
		 {
		  temp=word[a];
		  word[a]=word[b];
		  word[b]=temp;
		  printf("\n\t\t\t\RESULT:");
		  puts( word);
		 }
		 }
		 }
		 getch();
		 clrscr();
		 printf("\nYOUR WORD IS:");
		 puts (dup);
	break;
       }
       case '4': {
	   if(strlen(word)%2==0)
		   {
		     for(i=0;i<strlen(word)/2;i++)
		     word[i]==toupper(word[i]);
		     {
		     for(h=0;h<strlen(word)/2;h++)
		     word[h]==tolower(word[h]);

		     printf("UPPERCASE …
Adak 419 Nearly a Posting Virtuoso

It's really odd how having no code for upper or lower case changing, means no result. ;)

Adak 419 Nearly a Posting Virtuoso

If you want any respect on a programming forum, I'd change your forum handle.

Outside links to "alice", won't go over well, either.

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Happy! ;)

Tip: Next time, start up a thread all your own, instead of posting your problem, in another thread.

So what have you got to sort? Can you post a little example of the data, or describe it?

Also, how MANY things do you have to sort? There's a big difference between sorting 10 things, and sorting 10 million! Different algorithms are used.

Adak 419 Nearly a Posting Virtuoso

PLease help me !!
how can i uppercase the half only of the string and the half is in lower case??

include string.h, and ctype.h, and use the strlen(string) function to help you know the length of your string. Then divide by two. Add one if the length is odd (13/2=6, but the other half will be 7, not 6 char's in length).

Then use a for loop from 0 to half-1:

for(i=0;i<half;i++)
  charArray[i] = toupper(charArray[i]);

Use similar logic for the other half, and tolower(), in the same way, starting at half and ending at length of the string in charArray[].

The way it works on the forum, is that you need to post up your code FIRST, and state what the problem is, if you want help that includes specific code.

And always put your code between code tags (click on

icon in the editor window)[code]
icon in the editor window)

Adak 419 Nearly a Posting Virtuoso

When you run across the merry prankster who convinced you to use that sorting code, kick 'em in the a**.

I've looked through this a few times, and it's a toss up between cussing and laughing, I can tell you. ;)

By all means, narrow down the problem! The worst thing in programming, is to have 100 lines of code, in several functions, and then find out "oops!", and have NO IDEA where the error is.

Check major blocks of code, and all functions, as you're programming:

1) Are they syntactically correct (compile w/o errors)?
2) Are they accurate across the range of data you'll be dealing with?

You'd be amazed at how often stupid errors are not found early on, easily, simply because the programmer was "in a hurry". Later, it takes 5 X as long to find the error.

Adak 419 Nearly a Posting Virtuoso

Change the %d print format to the correct %c, and see how it looks. ;)

Can you give some of the details of what you're trying to find in the text?

Adak 419 Nearly a Posting Virtuoso

A couple big time savers:

1) You only need to check a number up to (and including) the square root of that number. If the number is prime at that time, then it's prime.

2) increment your loop by 2 for the numbers to be checked for primeness. Even numbers can never be prime if they are higher than 2.

You can read up on the Sieve of Eratosthenes at Wikipedia.

Adak 419 Nearly a Posting Virtuoso

Probably won't work, because the web site will only allow you to enter a password attempt 3-5 times. Then you're out of play for an hour or more.

And no, it doesn't sound like a homework assignment. ;)

Adak 419 Nearly a Posting Virtuoso

Using strchr() or strstr(), along with a smart search logic to keep it fast, is what I'd recommend. Boyer-Moore algorithm is tops, but may be much more than what you need.

Your compiler has a BUFSIZE that will be a good value to start with for your buffer array size.

Adak 419 Nearly a Posting Virtuoso

I have also Googled quite a bit, (8 pages of links), and have not found code in C showing the EIC.

What might be possible to code from given pseudo code? Depends who's doing the coding, and whether they were familiar with the logic of that pseudo code.

Which leads me to recommending that you direct your questions in this general cryptographic area, to forums that focus on cryptology, like sci.crypt on Usenet (also available through Google groups I believe). What you want isn't just C, and it isn't just simple cipher help (Playfair, Vignere, etc.). It's complex, detailed, and you want a particular flavor of a particular cipher, coded up.

That is just not going to happen in a C forum, imo.

Google up some other forums, and see if your questions can be given some better answers. It's just not C-focused, enough to get members involved on this forum.

Adak 419 Nearly a Posting Virtuoso

If you'll post up a small example of the file, as an attachment, we can show it.

I'm not sure what is included in Code Snippets.

Adak 419 Nearly a Posting Virtuoso

All operators (and thus all mathematics operators), are tokens. Your compiler help should have a list of them.