Adak 419 Nearly a Posting Virtuoso

OMG...it worked...!!thanx a lot...

You don't have to act << SO >> surprised!! ;) ;)

This is just the first part, of course. Lots more to be done.

Adak 419 Nearly a Posting Virtuoso

As soon as you can manage it, move away from the scanf() and fscanf() functions. They are not robust, and will quit for the smallest variance, sometimes.

the answer is to use:

fgets(stringBuffer, sizeof(stringBuffer), stdin (or filePointer) );

It works remarkably well, the file or the user can NOT over-flow the stringBuffer, and your data is THERE - you can test it 16 ways to Sunday, if need be, to get what you want!

One caveat: If you are in a called function, and use sizeof(YourArray), you WON'T get the size you expect. You'll get the size of the POINTER that you passed to the function. If you want to work with the actual size of the array in a called function, you must pass that number, as a parameter, to the function.


There was a big misunderstanding about feof(). It doesn't work the way a lot of authors and teachers thought it worked. Frequently causing the last data to be processed twice, before it finds the EOF.

Testing your return, inside a while() loop is preferred, and avoids the problem.

Turbo C is more compatible than you think, I think. ;)

I wouldn't recommend it, but if you need to use it, don't worry, you can learn a LOT using it.

Adak 419 Nearly a Posting Virtuoso

I've thought this through a bit more. You can do this with your current compiler. It's not as easy, but here's how:

Make the bigNum a long double data type. Add #include <math.h> for the floating point packages (doubles and floats both).

Now take the sqrt() of bigNum (800 651 475 143.00), and put that sqrt() into another long double variable. Let's call that variable numbr.

Then an unsigned long int num = numbr / 1; //yes, that's a one

Which will get rid of all the digits after the decimal place, and we'll have the right number we want - 775,146.

That's the highest possible prime number less than bigNum. Now you can deal with a much smaller number, and avoid bigNum, for now. An unsigned long int will handle 775,146 very easily.

All you have to do now, is find the largest prime number below 775,146, that is also evenly divisible into bigNum.

Once we have all the prime numbers, we can use an int array to test those numbers, and just repeatedly subtract to simulate division. (Much easier than doing long division with int arrays).

It won't be blazing fast, or all that easy, but if you can't find other solutions you like, it's a way to do it.

This is a bit muddled, but shows getting to the sqrt() of the bigNum, and putting it into an unsigned long int. This was done on an old 16 …

Adak 419 Nearly a Posting Virtuoso

The programs I posted shows how it can work.

You're welcome.

Adak 419 Nearly a Posting Virtuoso

As a practical matter, I'd use C date functions, but as an exercise, it looks OK. I'd use a static int variable first_flag to malloc the array just one time, and then not free it until the loop was all through.

I'd say you've got your C basics pretty well working, here. ;)

Adak 419 Nearly a Posting Virtuoso

You're welcome, and let us know how you're doing.

Adak 419 Nearly a Posting Virtuoso

Hmmmm...

Any hints from the book or teacher?

What is your operating system?

I know the 64 bit compilers (and maybe some 32 bit compilers), will allow long long int's, but I'm not sure which one's, exactly.

Might be a way of doing this using a string of individual int's in an int array, but I'm not familiar with it.

OK, well a few things to look for then:

1) A 64 bit compiler that will run on your OS, and (hopefully free) and has long long int's.

and

2) A way to work with int's in an int array, to use the array, as our big number

3) GNU has a BIG INT library that I'm sure could handle this. Is it OK for you to use a library like that, in your program?

Adak 419 Nearly a Posting Virtuoso

Did you try using "long long int" and "unsigned long long int"?

Adak 419 Nearly a Posting Virtuoso

I'm not familiar with C-free, but it appears to be in a cpp (C++) directory. Gcc should have a long long int, and hopefully, an unsigned long long int.

It's going to be quite difficult to find a prime number, if we can't find a data type that we can use to hold the number for us. I don't know of any way to find a prime number, of a string. (Might be a way, but I haven't thought of one yet).

Did your teacher or book talk about some way to use a string as a number to find it's prime factor?

There are BIG INT libraries of course, but I doubt if that's what was intended for you to use.

Adak 419 Nearly a Posting Virtuoso

What compiler are you using?

Adak 419 Nearly a Posting Virtuoso

C has functions to print up, or help you print up dates, in a variety of formats.

So free the memory, afterward. As long as you have the right address, what function you free the memory in, won't matter to the OS.

There's no need for the cast you have on the pointer from malloc(). C does that for you correctly, in this case.

for numbers in a field two columns wide, with 0's padding in front as needed:

printf("%02d", number);
Mouche commented: Quick and helpful reply with a simple solution +4
Adak 419 Nearly a Posting Virtuoso

Does your compiler support type long long int, or unsigned long long int, and will it now handle your 12 digit integer, OK?

Adak 419 Nearly a Posting Virtuoso

Edit: Does your compiler support long long int's? or even unsigned long long int's? Try those out.


Now, let's think about finding this greatest prime factor.

You aren't REALLY going to search for a prime number less than N, and ACTUALLY go all the way up to N are you? :(

Do a little Googling, and come back and tell me where the highest number you need to search, really is?

Then we'll chat some more.

Adak 419 Nearly a Posting Virtuoso

Looks like your compiler is telling your to use unsigned long int. Try that, one time.

*Always pay attention closely to any compiler error or warnings!*

Learning to debug is an important part of programming, and using compiler messages is a critical part of that.

Adak 419 Nearly a Posting Virtuoso

You will find a lot of programs using C syntax, in the "snippets" section of the forum (use the pull down menu's to get there).

Naturally, there are other bits of code, all over this forum - so help yourself, and if you have any questions, fire away! ;)

Adak 419 Nearly a Posting Virtuoso

Study your manual that came with the micro controller. What you can do depends on the system set up, and what compiler is made to run on it, etc.

Adak 419 Nearly a Posting Virtuoso

Srinivasan, you should know that we get a lot of students just asking for "the code", to save them having to do any work.

Atomic33 is such a poster, so far. He's put no work into this thread.

If you give out programs to these kinds of posters, the forum will be plagued by more of them, before long.

Make them show some work. Sometimes I'll give an incomplete program if the logic is rather involved.

Ancient Dragon commented: Exactly +33
Adak 419 Nearly a Posting Virtuoso

If you have a string, you can start atoi(), wherever you want. It just needs a pointer to the right starting point in the array

For example:

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

int main() {
  int i, n; 
  char snum[]="12345";

  printf("\n\n Original String: %s", snum);

  n = atoi(&snum[2]);
  printf("\n\n New Number is: %d", n);

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

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

Just make sure it has an end of string char: \0, at the end of the numbers. If it doesn't have that EOS char, then you'll have to do it in a loop with the specific indeces you want included.

Adak 419 Nearly a Posting Virtuoso

This isn't a full program, and it's VERY rough, but it shows the logic that would be hard to understand in pseudo code. I would use logic like this.

#include <stdio.h>
#include <string.h>

int main() {
  int i, j, digit, len, num, power; 
  char snum[10]={"1234"};
  char ans[100]={""};
  char pwr[4][10]={ {"one"}, {"ty"},{"hundred"},{"thousand"} };
  char wrd[20][20]={ {"zero"}, {"one"},{"two"},{"three"},{"four"},{"five"},
  {"six"},{"seven"},{"eight"},{"nine"},{"ten"},{"eleven"},{"twelve"},{"thirteen"},{"fourteen"},
  {"fifteen"},{"sixteen"},{"seventeen"},{"eighteen"},{"nineteen"} };
  // 012345678901234567890123456789012345678901234567890
  //(seventy seven thousand seven hundred, seventy seven) -- 50 char's
  // 15 spaces before thousands, 30 before hundreds

//  Marked out while debugging, only:
//  printf("\n\n Enter a number from 0 to 10,000: ");
//  scanf("%s", snum);
//  i = getchar();  
  
  printf("\nNumber is: %s", snum);
  len = strlen(snum);
  j=0;

  for(i=0;i<len;i++) {
    digit=snum[i]-'0';   //get the next digit from the string
    strcpy(&ans[j], &wrd[digit][0]);  //use it as an index to the wrd array
    j += strlen(wrd[digit]);       //adjust j;
    ans[j++]=' ';                //remove end of string marker char and j++

    //add the right power word here  //unfinished

  }
  printf("\n\n Answer: %s", ans);

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

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

OK, you start us off - how would you do this?

Adak 419 Nearly a Posting Virtuoso

SHOW ME AN EXAMPLE OF THE ARRAY, AND IT'S DATA, AND WHAT YOU WANT OUT OF IT.

Your descriptions are not adequate in this case.

Don't worry about atoi(). Let's learn about the problem. The solution will present itself afterward, clearly.

Adak 419 Nearly a Posting Virtuoso

I'd be tempted to use a small 2D word array:

powr[] array:
[0]="teen"
[1]="ty"
[2]="hundred"
[3]="thousand"
etc.
wrd[] array:
0="zero"
1="one"
2="two"
etc.

and this might easily change. I'm not familiar with this exercise.

and put together the string answer, from big to small number values:
ans[100]={""};
and use a small char array for the number, itself:
snum[10]={""};

You know how to "peel" off the digits, so that could be useful
As much as possible, I'd want to use the association of the pwr and wrd array index, with the right word, instead of long lines of explicit code. Go Loops! ;)

Some explicit code will be needed, I believe, but limit it.

Let me mess around with this a bit. Thinking along more representative lines of working with this problem, what do you see?

99% of the time, when you feel the code is just being pedantic as hell, you're right - it is, and it needs a better algorithm. You're there, I can tell. ;)

P.S: *IMPORTANT*
If you want people on the forum to study your code - highlight it, and click on the

icon at the top of the editing window.

Otherwise your code looks like html crap, and is very hard to even look at, let alone study. ALWAYS USE CODE TAGS around your code.[code]
icon at the top of the editing window.

Otherwise your code looks like html …

Adak 419 Nearly a Posting Virtuoso

Using strtoi() or atoi(), comes to mind (include stdlib.h).

If the 3D part of it has your head stretched out sideways, post a very small example showing your problem, and what you want out of the data, for an example.

Adak 419 Nearly a Posting Virtuoso

He made a program to display a single picture of micky mouse, that you can see in the earlier part of this thread.

It was just a joke reply to the OP. The only program with animation was the one with the primitive looking car, right near the last post in the original thread.

If you're looking for an animation program specifically, look at the one with the car, and then start a new thread with what you want.

This old a thread, is not going to get much attention, imo. You're already on page 4, and you've really not gotten started on anything of your program, yet.

I will not be posting to this thread, in the future.

Adak 419 Nearly a Posting Virtuoso

I don't believe you can make any generalizations about compiler efficiency, without testing them, as done by Ancient Dragon, above.

Then you have facts. Without testing, you have hot air, and smoke blowing up your arse. *, and a bunch of unsubstantiated beliefs.

*A reference to the old belief that a person who drowned could be revived by blowing tobacco smoke up their anus.

Adak 419 Nearly a Posting Virtuoso

We got off on the wrong track, I think - you tricked us!

What notation for the answer, can you use? Because I don't think your teacher wants all those digits being printed out, if you printed out the answer in base 10 (like a normal number).

So maybe engineering notation, would be OK? printf() will format an answer for engineering notation output.

So MAYBE the assignment is not to do a lot with a big integer array, maybe it's to set up printf() to print out the answer, with the right format?

If I said that in binary, the columns (or "places") have the following values:

2^5, 2^4, 2^3, 2^2, 2^1, 2^0

would that stir up any idea's for you?

Can you post some examples from your class notes? That would be best. The details of the way the assignment is phrased, could make all the difference here.

Adak 419 Nearly a Posting Virtuoso

Micky Mouse was just a picture, not an animation. It's on a previous page of this thread.

But I'm Adak, not Nick.

Adak 419 Nearly a Posting Virtuoso

You might look at it as an array of integers. Each digit in the number, then is assigned to an element of the array:

456 becomes a[4][5][6] in it's first three elements, or,
a[0] = 4, a[1] = 5, and a[2] = 6.

You can do about the same thing, using a char array, but it's just a bit more complicated.

You will have to keep track of how many digits you have in your array OR you could give your last (highest) array element, a number like MAXINT (which is the maximum value that your integer can have. You need to add the include file <valuess.h> to your program, in my case. Check with your compiler and see what name your maximum integer value, goes by: MAX_INT is also popular, and what header file you need to include, to use that value.

EDIT: I like Creeps idea for determining how many digits belong to the number, also. Very nice. :)

Now, if I have a value like 18 trillion, I can add it to my int array, by peeling off the digits, one at a time, until all the digits are used up:

How could I use the mod operator, %, and division by 10, to "peel" off digits from the right hand side of a number, one at a time, in a while loop?

Adak 419 Nearly a Posting Virtuoso

Where *could* that program be?

1: Left of here? Naw, nothing interesting there.

2: Right of here? Nope, just the right margin of the window

3: Down from here? Nope. Only newer posts are there, and you're looking for an older post.

So where could that program be?

For the love of Gawd, *where* could that program BE?

;) ;) ;)

And Welcome to the forum, Shane! For specific help with your program, please start a new thread - this one is quite old, and be specific about what the problem(s) might be.

Adak 419 Nearly a Posting Virtuoso

Yes it might - simple lines of code will be evaluated perfectly, by any compiler worth a damn. There will be no differences there.

Better compilers are able to evaluate more complicated lines of code or blocks of code. Their output will be more efficient and thus will run faster, on most or nearly all programs, compared to more primitive compilers.

Adak 419 Nearly a Posting Virtuoso

If you have some code, and a question about that code, I'm happy to try and help. In this case, you need, it seems to me, a tutorial.

There are many excellent tutorials on the Web, both text based and video - some even from major universities.

Hiyaki over that way, dude - that be whar yur treasure truly lie! Haargh!! ;)

Adak 419 Nearly a Posting Virtuoso

Depends on how smart the compiler is.

A modern compiler should be able to deduce that the compiled code needed, is the same.

Adak 419 Nearly a Posting Virtuoso

This is a static array declaration:

int array[50];

The memory comes from a block the compiler sets aside called the "stack". it's convenient, but 1) lasts only until the function that called it goes out of scope, (for main() that's forever), and 2) the total memory available is about 2 MB, max.

This is an example of a dynamic array:

int *array;
array = malloc(50 * sizeof(int));

These arrays can be can be multi-dimensional (same as a static array in that regard), and can take all the rest of the "heap" portion of memory, set aside by the compiler. Typically, that's MUCH larger, depending on the system you're on (the hardware), and the memory model the compiler is using, and the size the compiler is coded for. A 32 bit compiler will not be able to access nearly as much memory as a 64 bit compiler, even on the same system, with lots of memory.

Dynamic data remains until either the program ends, or you explicitly free() it. It never goes out of scope.

You need to include stdlib.h for malloc(). While static arrays are set to 0 or "", by most compilers, malloc()'d arrays are not, generally. Use calloc() if you need to set the memory to zero or NULL.

There are special considerations for dealing with multi-dimensional dynamic arrays, and arrays of structs with dynamic arrays inside them -- like yours. Those inner dynamic arrays (as Narue showed you), must be allocated …

Adak 419 Nearly a Posting Virtuoso

Ideas:

#1 What is the system RAM size? WinXP can use only a bit more than 2Gigs, (it's only 32 bit)but Win7 and Linux (64 bit versions of each), can use a LOT more memory. For speed, that would definitely be the way to go. Ubuntu 64 bit is one I've used. The live CD you d/l makes it relatively painless, sets up a multi-boot loader by default, and has a very active help forum as well.

#2 The alternative is to somehow break up the problem, and work with "chunks" of say, 200,000, each or 4 chunks. (any even number of equal sized chunks is best). Say you want to sort all the records, to quickly remove any duplicates (or do some work).

You can make a multi-tier record handler:

File 15
                               /        \
                             /            \  
                           /                \
                   file 13                    file 14
                  /       \                  /        \
           file 9        file 10         file 11         file 12   
          /      \      /      \       /      \        /      \
        file 1 file 2  file 3 file 4  file 5 file 6  file 7 file 8

I used logic like this, (but more "tiers"), to handle 50,000 records when I was using a 16 bit compiler. It had only 64KB of memory the compiler could access. This can handle merging (with or w/o sorting), the smaller chunks of memory, using files on modern HD's, (with their big cache's), much faster than I expected.

From experience, using the latest i5 or i7 Intel …

Adak 419 Nearly a Posting Virtuoso

I'm not sure why you want to delete white spaces??

This is a two year old thread, so it's best to start a new thread, and perhaps tell us what you are trying to do, in more detail.

If you will show me your code, I will show you mine. ;) I call the function "compress", and sometimes "boogie_left". All it does is just take a string of chars, and delete any spaces from it.

Adak 419 Nearly a Posting Virtuoso

Unfortunately, you didn't read the "read me before posting" sticky thread, before posting, and never understood just what it is that we do here.

This is a great resource, but you have to know what it does - and does not - do. Once your understand that, you can use this forum very well for help with questions specifically related to C code.

Unfortunately, we get asked to answer other questions a great deal, by people who could find REAMS of data about the subject, on Google, and have no code issues whatsoever. Many just want the answers, wrapped up with a ribbon - thank you very much.

Civility suffers as a result. (as Narue's sig plainly shows).

Adak 419 Nearly a Posting Virtuoso

That's how we programmed the mass tuples checking program, also. It worked very well in our tests.

j should be assigned the value of i+1, rather than i, and i should have a terminating test of i<x-1, instead of i<x.

Have you found out what your largest array you could allocate, yet?

Adak 419 Nearly a Posting Virtuoso

OK, I've checked it out - big assignment!

You better get started, eh? ;)

Post back when you have a question about your C program.

Adak 419 Nearly a Posting Virtuoso

Praveen, please start a new thread for your question, and have someone help you with the English OK?

I don't know what you are asking.

Adak 419 Nearly a Posting Virtuoso

malloc (and calloc), are slow, compared to static arrays. But if you need a BIG array, then it should be the way to go.

You have to consider:

1) there are advantages to allocating memory all at once, instead of "every time through the loop, allocating just enough for one record".

2) test your system, and see how large you can allocate your array, without slowing down your whole system.

3) can you trim down the number of records you need to have in memory at one time, to work within the size you saw in your test in #2?

4) C is faster. 4.6 X faster than C# in a test involving mass testing a few hundred thousand tuples. (Where tuples with 2 matching values had to be found from amongst all the other tuples).:

http://cboard.cprogramming.com/c-programming/129611-c-real-life.html

is a summary of the 5 page thread (which is a fun read itself, and also linked).

WAY faster than Python and other interpreted languages, in general.

Each idea with merit, should be tested.

Can you give more details of what the program is doing, and maybe a sample data file for testing?

Perhaps post a file that takes 5 seconds or so to process in Pearl. Then we can compare that with what we find best, for C.

If you post the data file to Swoopshare (a free file depot), and then post the url to that file, …

Adak 419 Nearly a Posting Virtuoso

Paula - it's OK. I had to change your program because I didn't have the "m" file. The logic is now OK, so just use your lines of code that I //marked out, and go ahead and mark out (or delete as you wish). My lines of code that relate to the keyboard entry.

Replace "stdin" in the fgets() statement, with M, and remove the // from the fopen() line of code.

If you have any other questions, ask away.

Adak 419 Nearly a Posting Virtuoso

I don't have the "m" file, but this works via keyboard entry.

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

struct estr_reg {
  char *molid;
  char *molc;
};

int main() {

  FILE *M;
  char *ptr;
  char line1 [ 540 ]={""};
  int i=0, numEntries;
  struct estr_reg reg[6];

  //M=fopen( "m", "r");
  printf("\n Enter a number followed by a semi-colon, and then a name.");
  printf("\n Enter q to quit entry. \n");
  while ((fgets(line1, sizeof line1, stdin)) != NULL )
  {
    if(line1[0]=='q')
      break;
    ptr=strtok(line1,";");
    strcpy(reg[i].molid, ptr);
    ptr=strtok(NULL,";");
    strcpy(reg[i].molc, ptr);
    i++;
  };
  //fclose ( M );
  numEntries=i;
  for(i=0;i<numEntries;i++)
    printf("%s %s", reg[i].molid,reg[i].molc);
  printf("\n\n\t\t\t    press enter when ready");
  i=getchar();
  return 0;
}
Adak 419 Nearly a Posting Virtuoso

I typed the flipping wrong WORD - should have been "typedef", not typecasting!

/face palm

Adak 419 Nearly a Posting Virtuoso

First, Narue is a "ma'am" not a "mister", and what we do is help people with their C code.

You post your code, and say what's not working right, and we try to find out what's wrong with it.

There are homework sites, but this isn't one, "per se". We don't help those who just ask for "the codz", kind of thing. We wind up helping a lot of students, of course.

Adak 419 Nearly a Posting Virtuoso

And after raising his MaxLength to over 30; hello overflow.

#define MaxLength 30 //or 60, or whatever he needs?
or
const int MaxLength=30;  //or 60, or whatever.

and then:

word[10][MaxLength];

There can be data being truncated, but there can be no overflow as long as he uses MaxLength to set his size, in fgets().

After you put the string into the buffer with fgets(), you would get it's length in char's, with strlen(), and then malloc or calloc that many chars. You need stdlib.h for these functions.

Anything you malloc or calloc, you need to free(bufferName), or you run a risk of having a memory "leak" of lost memory, on that system. (depending on your OS and system).

Adak 419 Nearly a Posting Virtuoso

I appreciate your explanation, Narue. I learned from two books, primarily:
"The C Programming Language" - K&R and "Beginning C" - Horton.

Both describe typedef as making a "synonym" or "another name", for a data type: K&R p.146, Horton, p.416., but they're much more than just a define, and that would include casting characteristics.

Casting is something I'm just abysmal with, on the whole. The less I have to do it, the happier I be. ;)

Adak 419 Nearly a Posting Virtuoso

Wow, this one is bending my head around like the possessed girl in "Exorcist"!

It isn't putting the data anywhere else but on the first element of the array of structs, even inside the while loop.

Right now, I just don't see it. Wish I had more time right now for it. :(

The second reg.moldid, should be reg.modc, imo, but that's not a biggy right now. I'll pass it up the chain to some friends. See what they say.

Would you post up a few lines of text from the m file, so I know I've got the right data for it?

Adak 419 Nearly a Posting Virtuoso

Well...

Forget what I was saying about a valid address -- your program has no main() function, so it had no insertion address, itself.

Let's start with this, for your code, and work on it, from there:

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

struct estr_reg {
char *molid;
char *molc;
};
int main() {

  FILE *M;
  char line [ 540 ];
  long int i=0;
  struct estr_reg reg[6];

  M=fopen( "m", "r");

  while ( fgets ( line, sizeof line, M ) != NULL )
  {
    reg[i].molid=strtok(line,";");
    reg[i].molc=strtok(NULL,";");
    i++;
  };
  fclose ( M );
  printf("%s %s", reg[0].molid,reg[1].molid);
  printf("\n\n\t\t\t    press enter when ready");
  i=getchar();
  return 0;
}

That will at least compile. Back in a bit.

Adak 419 Nearly a Posting Virtuoso

I'm assuming he's not a brain dead yokel that just fell off the turnip truck yesterday, and will adjust MaxLength to whatever size he needs.

Of course.

Adak 419 Nearly a Posting Virtuoso

Note: non-decreasing == ascending order ;)

Fewest number of comparisons: Insertion sort.
Fewest number of swaps: Selection sort.
No comparisons at all: Counting sort (if the data is ranged OK for it)

So we're not trying to move the values (like using a sort), we're adjusting their values, and counting the cost incurred, until the numbers are in sorted order?

Hmmmmmm. . .