jephthah 1,888 Posting Maven

thank you for dropping your load of crap in a 5 year old thread that was already resolved and put to bed.

The next time you wish to drop another load, let me suggest that you start a new thread all on your own. That way you won't soil someone else's thread, you won't confuse other students else with your broken, non-portable junk, and we can tell you why your code is crap.

meanwhile: request thread closed.

jephthah 1,888 Posting Maven

But... the compiler return always zero.

the compiler is not returning zero. the compiler is what builds your code into a binary executable after linking the appropriate functions from teh standard libraries. the binary (program) is thing that, during runtime, returns your values of zero or otherwise.

so, you know, I'm just sayin'.

otherwise ... what Salem says ^


.

jephthah 1,888 Posting Maven

i fail to see how your refusal to pay attention in class and/or read the material somehow translates into an urgent need for me to do your schoolwork.

jephthah 1,888 Posting Maven

dude, this is really freakin easy.

360 degrees in a circle.

60 minutes in a circle

6 degrees per minute

1 degree per 10 seconds

0.1 degree per second.

you can have 10 second resolution and only use integers.

if you need 1 second resolution, you're still only in tenths of degree, the math is elementary.

jephthah 1,888 Posting Maven

ain't nobody here but us chickens

jephthah 1,888 Posting Maven

The problem was that the size I was passing in the first function was a incorrect size and it had problems.

This is something simple so gets works OK for this :)

NO. You're unfortunately, understandably, but undeniably WRONG.

the PROBLEM was that you were using gets() in the first place.

gets() is never OK. ever. so get it out of your head.

jephthah 1,888 Posting Maven

How to use time.h functions? all you need to know is there

jephthah 1,888 Posting Maven

Oh....

:(

What a sad loss. I will miss him. I liked him very much, and benefited many times from his knowledge,.

I regret my getting prickly towards him in some political discussions. He was never anything but polite and just loved the debate. I wish now I was better able to see past the superficial and had better known the man, the husband, the father ... the person ... behind the screen name.

Deepest regards and sympathies to his family. Dave was well liked and widely respected.

Long time no C

Indeed. Godspeed, man.

.

jephthah 1,888 Posting Maven

okay, i've changed my mind. I would buy a daniweb shirt that said either

"Banned" or "givemetehcodez"

cause that brings teh funnay.

.

jephthah 1,888 Posting Maven

Title: Strange bug with gets()

Answer: the "gets" function *IS* a bug. kill it before it breeds.

Aia commented: Read the title. Thought the same. +8
jephthah 1,888 Posting Maven

thanks, but that's already been dismissed as a solution. we cant go an edit DHF files in a FDA-regulated environment after the fact.

also you're about 2 months late.

thanks anyhow, tho.

jephthah 1,888 Posting Maven

your variables.c file should NOT have a "main()" function. your variables.c file should have ONLY the implementation of the three functions prototyped in variables.h.

the two files, variables.c and variables.h are a pair. the .h declares the prototypes, the .c defines the implementation. you may not be used to seeing .h files, they are not strictly necessary. but you will find they are increasingly important as your projects get bigger and more files are involved.

the "dictionary_test" file is the file that has the "main()" routine. this is the entry point when the program is executed. this file will have the "#include variables.h" which links the functions compiled from the "variables.c" that you will write, so that those functions can be used in the program.

your job now is to write these three functions, put them in the "variables.c" file, and write them strictly according to the prototypes declared in "variables.h"

here is the start of your variables.c file:

#include <stdio.h>


void set_variable (const char* name, double value)
{


    return;
}
double variable_value(const char* name)
{


    return 0;
}

void display_variables()
{


    return;
}

your project will now compile without errors. it won't do anything, and likely may crash with a runtime error until you actually write the functions to do what is expected, but it will compile.

try to understand this general method. if you ever get a job programming, this is likely how you will be assigned work to do: to write …

jephthah 1,888 Posting Maven

line 4

*ptr2Data = (unsigned long) SomeArrayOfData[4];

jephthah 1,888 Posting Maven

If you're planning to profit on these t shirts (which I assume you are) then you should also think of some normal-ish ones for people like me who wouldn't buy the "geeky sayings" ones.

yeah, i don't think they're going to profit on this. it's just SWAG to get name visibility.

and sorry, i don't think i would wear any of those. "geek funny" is kind of late 20th Century.

The new internet funny is caustic and cynical. So wouldn't try to go for clever or funny sayings. Daniweb just doesnt bring Teh Funnay. Well, not intentionally anyhow.

We're mostly a homework help forum for the chronically helpless.

jephthah 1,888 Posting Maven

well, those can be excused becasue the poster thought they were posting a question correctly.

the worst is these code snippet "programs" that with deprecated Turbo C libraries: they don't compile, they're so horribly written they're unreadable, and even if you take out the non-standard stuff, they're still broken and don't do what they're supposed to do.


.

jephthah 1,888 Posting Maven

well, okay...... but we waited almost 3 years for that?

jephthah 1,888 Posting Maven

or perhaps something more along the lines of

jephthah 1,888 Posting Maven

i think a peer review is absolutely necessary for tutorials.

i mean, just look at the sad state of "code snippets"... there's some horrible, non-working stuff in there.

if i didn't know this site, and i came across some of those snippets, i would be inclined to think they were sanctioned by the admin, and that this whole place was a joke and run by idiots.

jephthah 1,888 Posting Maven

probably: fat, loud, incurious, and expecting the rest of the world to cater to our whims.

it's the global stereotype of americans held everywhere.

you gotta wonder why that is.


.

jephthah 1,888 Posting Maven

sorry i misunderstood the nature of the discussion, i thought you were trying to implement a more clever method to approximate COS.

...

as for "close_enough" don't make it harder than it needs to be.

say for instance, for 6 Taylor terms has your cosi() function returning the value of cosi( 1 * PI ) = -0.999971 (i don't know what it really returns)

and the *real* value of cos(), as implemented by the <math.h> library returns the value of cos ( 1 * PI ) = -1.000000 .

the close_enough() function just subtracts the two results and sees if the absolute value (magnitude) is less than 0.00005. if so, then it's "close enough". in the above made-up example, -0.999971 - (-1.000000) = 0.000029 so it is close enough.

this function is NOT part of cosi(). it's a separate function. the two arguments are the return values from cosi() and the math library's cos()

#include <math.h>

#define NUM_TAYLOR_TERMS                  6
#define PI                                3.14159

double approxVal, realVal, multiplier, theta;
int    isClose;

int main(void)
{
    //... get mulitplier input

    theta = PI * multiplier;

    approxVal = cosi(theta);
    realVal = cos(theta);

    //... print results

    isClose = close_enough(approxVal, realVal);

    printf("Using %d Taylor Series terms to compute cos(%06.4f) %s \"close enough\".\n",
                      NUM_TAYLOR_TERMS, theta, (isClose == 1) ? "is" : "is NOT");

    //... etc.
jephthah 1,888 Posting Maven

if this is a school assignment, i think you should stick with the Taylor Series.

6 Taylor Series terms can still fit the factorial part in a 32-bit int, and this is a "close enough" approximation for all Theta values from -PI to +PI, therefore covering all possible output values of COS()

if someone sticks in a theta multiplier greater than +1 (or less than -1), just subtract (or add) enough multiples of 2 from that multiplier to get the total theta angle back between -PI and +PI radians.

for instance

cos( 5.0 * PI) = cos( 1.0 * PI)  -->   3.0 - ( 2 * 2) =  1.0
cos( 3.0 * PI) = cos( 1.0 * PI)  -->   3.0 - ( 1 * 2) =  1.0
cos( 2.4 * PI) = cos( 0.4 * PI)  -->   2.4 - ( 1 * 2) =  0.4
cos( 1.5 * PI) = cos(-0.5 * PI)  -->   1.5 - ( 1 * 2) = -0.5
cos(-1.5 * PI) = cos( 0.5 * PI)  -->  -1.5 - (-1 * 2) =  0.5
cos(-3.9 * PI) = cos( 0.1 * PI)  -->  -3.9 - (-2 * 2) =  0.1

cos(77.5 * PI) = cos( 1.5 * PI)  -->  77.5 - (38 * 2) =  1.5

of course there is benefit in learning new ways, so i'm not saying don't try it. but you it might be "easier" and you get the appropriate answer for your assignment to stick to the well-understood method.


.

jephthah 1,888 Posting Maven

sorry it took me so long to get back to this.. You should learn how to use strtol(). each time you call strtol, it finds the first numeric value in a string, converts it to a long int and returns it. it sets the pointer argument to the very next character in that string

if non-numeric characters (alpha or symbols) are found, zero is returned as the value and the pointer continues to point at the start of the string that was being looked at. this indicates that it failed to find a numeric in the string.

(Note, strtol() will ignore and skip over any whitespace found before a value. whitespace after an int value indicates the end of that value.)

i'm going to give you this snippet because i think seeing an example like this in action is really instructive.

// ... set up your variables

fgets(single_line, sizeof(single_line), stdin)

numberOfEntries = strtol(single_line, &ptr, 10)

for (i = 0; i < numberOfEntries ; i++)

   value[i] = strtol(ptr, &ptr, 10) 

// ... do the rest

some people may think i'm giving you too much, so you're going to need to figure out from here how to set it up properly within the context of your program.

Be advised this performs no error checking whatsoever; erroneous input may cause this code to behave quite badly. all good code should have error checking.

for reference:
fgets()
strtol()


.

jephthah 1,888 Posting Maven

I have a fleet of cars -- it's a Ford.
I have a pride of lions -- his name is Leo

http://icanhascheezburger.files.wordpress.com/2007/05/jesus_christ_its_a_lion.jpg

Salem commented: ICHC FTW :) +20
WaltP commented: And the lion's wearing socks! +11
jephthah 1,888 Posting Maven

Sorry about the lack of the proper programming style.

I've just started learning programming and I'm not been really taught the proper way to do it.

yeah, and i was a little overly-bitchy. overall, you're on the right track: everyone has to learn somewhere, and "proper coding style" comes mostly with experience.

so, don't stress it too much. you're doing as well as a beginner should be expected. just keep it in mind that "readable code" is just as important as "working code". here's a few general principles:

-- make your variable names meaningful. don't be afraid to use long variable names. use capital letters and/or underscores to make multi-word variable names.

-- use #defines to name your "magic numbers", rather than just sticking them in the code with the hope that you'll remember what they mean 6 months later.

-- there's no single "best" bracketing style, but you do need to pick one bracketing style and always stick with it. don't mix it up.

-- make judicious use of whitespace to separate code functionality.

-- learn to make you code modular by creating subroutines that do particular tasks, rather than lumping everything under main()

Would this be a lot better?

not especially. fundamentally this is the same thing. any differences are matters of degree.

it still uses "magic numbers" that are cryptic and error-prone, variable names that are similarly meaningless to the reader, and is still a huge string of code that …

jephthah 1,888 Posting Maven

Hi. first a comment, then the answer:

the reason why it took 14 hours for someone to reply, is that your code is nearly unreadable. yes, you used code blocks and yes you used indentations, and that's a great start. thank you.

but your lack of a consistent style of bracketing, lack of a consistent style of indentation, and a seeming aversion to using whitespace or giving your variables meaningful names, well, all of that makes it very hard to understand what you're doing.

and if you think this is a pedantic criticism, it's not. unreadable code is worthless in the industry, even if it "works". companies lose millions of dollars and people lose jobs because code is not intelligible to others.

so, i felt sorry for you not getting an answer, but i had to fix your code to make it readable before i could spot the the (very simple) error.

here's your newly formatted code. merry christmas:

#include <stdio.h>
#include <time.h>

int main(void)
{

    int x, y[12][12] = {{0},{0}};
    int roll, move;

    int i, j;               // okay, these are loop indexes
    int a, b, c, d;         // but these are meaningless names here

    srand(time(NULL));

    a=5;
    b=5;
    y[5][5]=003;         // "magic numbers" are not good programming


    {  // <--- WHY IS THIS BRACKET HERE?

        for(i=1; i<=10;i++)
        {
            y[1][i] = 2;          // more magic numbers
            if(i==5 || i==6)      // okay, it just gets worse from here.
                y[1][i] = 0;      // so no more comments about …
tux4life commented: Nice :) +8
jephthah 1,888 Posting Maven

i heard Fortran is the next New Thing

jephthah 1,888 Posting Maven

okay, i see that duckman lives in the nether-regions.

i don't know what the employment system is like there,, but over here in the good ol US of A, anyone can get a job if they want it.

employers arent even allowed to ask your age until after you're hired.

age discrimination is hard to prove, but a company will get hammered with serious discrimination lawsuits if such practices were to be discovered.

the OP can rest assured that if he's moderately competent, he can get a job just about anywhere in IT. seriously, the jobs are a dime-a-dozen.

now he won't get the most prestigious ones right away, but even then if he works hard and plays the political/business games right, he can move up to almost anywhere.

jephthah 1,888 Posting Maven

thank you both for the input. and thanks jephthah for helping me clean it up a bit. I have one more question. It seems to be working for values of 0.5, 0.24, 0.166667, and 1. But why am I getting a cosine value of 2.4653 for an input of 2?

taylor series is an approximation. at n=0, the series is perfectly accurate. afterwards, everything else is an approximation.

the approximation will get better as you use more and more terms.

with 5 or 6 terms, the accuracy is "good enough" up to values of n=PI. the accuracy quickly degrades beyond 1*PI, and is totally unacceptable by the time you get to 2*PI.

if you increase the number of terms, the accuracy will hold out longer. however, as you increase the number of terms, the factorial in the denominator starts to get ridiculously huge. a 32-bit int will overflow after 6 terms. even a 64-bit int will overflow after 10 terms.

jephthah 1,888 Posting Maven

Line 22 should be rad = input*pi/180; (since pi rad = 180 degrees)

(also cos(2*pi) = 1 not 2)

pi / 180 is what you'd use if you were making the conversion from degrees. i don't think he is doing that, his input is just in radians, actually the input is the multiplier, like 1/4 * PI or 2 * PI, etc.

and yes, cos(2 * PI) = 1

using 6 taylor series terms will give you good values for all angles between -PI and +PI radians. however if you try to do 2*PI, you will need more series terms and the factorial will be too large for a 32-bit integer. any angles greater magnitude than +/- PI will have your series summation become increasingly inaccurate.

here's your corrected code. i think your problem was that you weren't alternately subtracting and adding the terms correctly. i cleaned up the indentation and teh debug output so it's more clear what's going on.

#include <stdio.h>

#define PI                3.14159   // make PI a #defined value, not a variable
#define TOTAL_SERIES      6         // the number of taylor series terms to use

#define DEBUG

float cosi(float x);
int factorial(int x);

main()
{
    /* Declarations */
    float input;
    double rad;
    double cosine;

    /* Ask user for an angle (in radians) */
    printf("\nEnter the angle multiplier in rads (don't include pi): ");

    /* While user has more input */
    while ( scanf("%f", &input) > 0)
    {
        /* Convert input into its decimal form …
jephthah 1,888 Posting Maven

'round here, it's also illegal to even ask how old a person is or to base a hiring decision on that info however obtained.

jephthah 1,888 Posting Maven

i'm curious what cities you visited. america is a diverse place, i think you may have been in a relatively conservative area of whatever city you went to. For example, i live in the city of Seattle. i would contradict some of your observations with anecdotes of my daily life.

-- there is no Wal-Mart in the city limits of Seattle. they've been zoned out, we don't want them here. you have to travel pretty far to the outskirts, 20+ miles, 1/2 hour drive time to get to one. my wife gets me to go there maybe once a year.

-- our freeways are big, and yet traffic congestion is a constant problem in every metro area. we keep building bigger freeways, but its never big enough. most of the cars on the road are occupied by a single-driver. we seriously need to reconsider transportation alternatives.

-- cyber crime, id theft, financial account breakins are highest in seattle than every other city (NY and SF are up there with us). it's blamed on the widespread practice of providing "free (unsecured) wifi". something does need to be done.

-- mcdonalds sucks. i dont know if it's the symptom or the cause, but "big quick and greasy" represents what's wrong with america. and i'm as guilty as anyone. a big mac sounds yummy right now :(

-- guns are a divisive issue. there are strong opinions on both sides.

-- people here LOVE to talk about …

lllllIllIlllI commented: Hahaha good points :) +0
jephthah 1,888 Posting Maven

I'll be brutally honest with you: you don't stand a chance.
Most companies won't hire anyone without experience when they're over say 25 years old. Too expensive.
And by the time you get your degree, you'll be 35+, at that age it's hard to get any job in IT at all.

i can't say how strongly i disagree with this, but let me take a stab at it:

that is pure bullshit.

getting a job is all about aptitude and -- most especially -- enthusiasm for the company and perseverance in applying.

yeh, maybe there are some companies who will look negatively on a 30-something new grad, but f--k them anyhow, you dont want to work for a place like that. for every place that would reject you based on being 10 years older than the average new grad, there are plenty of places who will hire you without even considering this non-issue, and give you the opp'ty to build your skills.

now, i realize the economy is still down, and hiring is suppressed generally speaking, but this is across the board in all industries.

i dont know where hte duckman works, but if he thinks that 30-35 year old entry level IT candidates are going to be broadly snubbed because they are in their 30's then he's got a tenuous grasp on the reality of industry hiring procedure.

look, just be aggressive in your job search and treat your job search …

jephthah 1,888 Posting Maven

no i meant the printf() that i added in my version of your code.

depending on environment, printf often blocks the output until a newline is received.

jephthah 1,888 Posting Maven

before i start trying to explain it, i need to understand what exactly the requirements are.

i see you tried to explain, above, but i apologize that i don't fully understand what you said,

jephthah 1,888 Posting Maven
jephthah 1,888 Posting Maven

Ctrl z is not a C function. IOW, the interpretation of ctrl-z is system dependent, and is not necessarily EOF. For example, on unix, ctrl-d is used as EOF. On systems where ctrl-z is used, it still doesn't always get interpreted as eof, as you have seen

for microsoft OS, ctrl-z is only EOF when it is found at the beginning of the input stream. in this case EOF is a negative value (-1). when ctrl-z is found in the middle of an input stream after text, it is interpreted as a non-printable ascii character, just like any other escaped character. in this case:

ctrl-a = 0x01
ctrl-b = 0x02
...
ctrl-z = 0x1A

you can test it yourself by trying different combinations of text and control characters. remember the printf() is blocking and will buffer the standard input until a newline is entered, so you'll have to hit <enter> after any given amount of text.

#include <stdio.h>

int main() {
    int c;

    while (1)
    {
        putchar(c = getchar());
        printf(" %c = %#X\n",c,c);
    }
    return 0;
}
popcorn99 commented: leet h4x0r +0
kvprajapati commented: helpful. +9
jephthah 1,888 Posting Maven

you can probably do it with scanf() in a complex and obscure and difficult-to-understand manner.

i don't know how.

but i do know how to do it with fgets() and atoi() in a simple, straightforward and easily-understood maner..

jephthah 1,888 Posting Maven

sorry, that line #12 had a typo. it should be

inputsExpected = 4;

i corrected it in the post above. change yours to just remove that "f" at the beginning of the finputsExpected

sorry for the confusion.

jephthah 1,888 Posting Maven

i figured it was a cut-and-paste typo .. they happen to everyone, don't sweat it too much. i almost didn't notice it, because its one of those that don't trigger a compiler warning. becoming able to see these improves w/ experience. just try to slow down.and not rush things when programming and debugging. sometimes you have to walk away and go do something different for a few hours and come back later. :)

also, i didn't note it, but changed your debug printfs... you were using %s when you needed to be using %c to print meaningful information.

good luck, and come back if you need more help :)

jephthah 1,888 Posting Maven

no it's not. it's terrible. it's ugly. and it's unportable. it wont even compile on a standard C compiler. i doubt it even works.

jephthah 1,888 Posting Maven

look this is just nuts, man.

now just stop what you're doing. seriously. stop. it's an unintelligible mess and painful to look at. C code is not supposed to look like that. it's an abomination.

for one thing, you don't need a variable for every single parenthesis and math operator. look at the the example i posted.

#include <stdio.h>

int main (void)
{
    int a, b, c, d;
    int inputsFound, inputsExpected;
    double result;
    
    printf("enter equation exactly as shown:\n");
    printf("( a + b ) / ( c * d )\n");
    
    /* should find four (4) variable from scan */
    inputsExpected = 4;   
    inputsFound = scanf("( %d + %d ) / ( %d * %d )", &a, &b, &c, &d);
    
    if (inputsFound != inputsExpected)
        printf("\ninvalid number of inputs found = %d\n", inputsFound);
        
    else 
    {
        result = ( a + b ) / (double)( c * d );
        printf("\n( %d + %d ) / ( %d * %d ) = %f\n", a, b, c, d, result);
    }
    
    return 0;
}

see how clear and coherent this is? now do yours like this.

jephthah 1,888 Posting Maven

sorry, that's incorrect.

from your own link that you posted:

In 1988, Park and Miller [1] suggested RNG with particular parameters n=2^31−1 = 2,147,483,647 (a Mersenne prime M31) and g=16,807 (a primitive root modulo M31), now known as MINSTD. Despite that MINSTD was later criticized by Marsaglia and Sullivan, [2] it is still in use today (in particular, in CarbonLib).

for another thing, you're forgetting the +1 increment which is required when using this method to perform the modulus using a Marsenne prime.


.

jephthah 1,888 Posting Maven

non-printable codes have the ascii values from 0 - 31

ascii codes for capital letters start at value 65. the charcter for value 65 is 'A'.

or in other words, a char 'A' has the value 65

if you have the values 0 - 31 in some value, you can't print them as they are... tehy don't print to the terminal. (except for newline tab and backspace, and maybe bell, but never mind that for now)

so print them instead as a sort of code:

^A
^B
^C
...etc...

consider this:

for (i = 0; i < 32; i++)
   printf("ASCII %02d  =  ^%c\n", i, ('A' + i));

if it doesnt make sense still, then put it in a test program and run it.

.

jephthah 1,888 Posting Maven

hey man, since your requirements specify that the user must enter the equation exactly as shown, then scanf() will be sufficient to obey the letter of the assignment.

you'll get to the rest later, for now, scanf is the quick way.

consider the case for the equation ( a + b ) / ( c * d ) .... here's how you'd implement it using scanf.

#include <stdio.h>

int main (void)
{
    int a, b, c, d, e, f, status, expected;
    double result;
    
    printf("enter equation exactly as shown:\n( a + b ) / ( c * d )\n");
    
    expected = 4;
    status = scanf("( %d + %d ) / ( %d * %d )", &a, &b, &c, &d);
    
    if (status != expected)
        printf("\ninvalid input, status = %d\n", status);
        
    else 
    {
        result = ( a + b ) / (double)( c * d );
        printf("\n( %d + %d ) / ( %d * %d ) = %f\n", a, b, c, d, result);
    }
    
    return 0;
}

now follow this format and apply it ot the rest of your assignment.

jephthah 1,888 Posting Maven

The problem is that it is not reading in any zero's. I manually traced the program and discovered that the bug lies in this section of presults.c

while(wt >= 1)
        {       /*  get msd, convert to char, and print it  */
                #ifdef DISPLAY
                write_char(int_to_dig(sig_dig_value(ans,wt)));
                #else
                putchar(int_to_dig(sig_dig_value(ans,wt)));
                #endif
                /*  strip the msd  */
                ans = supress_msd(ans,wt);
                /*  go on to next weight  */
                wt = weight(ans);
        }

Okay, dude.....

I AM REALLY REALLY SORRY for hijacking your thread to go off on some ranting tangent that has been mostly unhelpful

really, I am sorry. I don't know WTF my problem was. Your code, once i got over the fact that it uses some non-standard libraries (horrors!) , is actually very clearly written, very nicely formatted, and very easy to understand

your problem is both simple and sneaky.

the fact is that when you have a zero in a power of 10's place, the modulus does not care that it's there.

so 305 only finds the 3 and the 5.

if you use integer division like you're doing to find the Weight, you will always have this problem.

the good news is, you really only need to call the weight function once, to get the highest power of 10. every power for the remaining digits is just one less power of 10. you dont need to calculate it, just decrement each time after the initial calculation:

while(wt >= 1)
        {       /*  get msd, convert …
jephthah 1,888 Posting Maven

try this, this does the weight according to your function...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include "presults.h"

main()
{
    /* Declare variables */
    int integer, valid, i;
    char weight[16], numStr[16], suppress_msd[16];  // oversized

    /* Prompt user for input */
    printf("Enter integer: ");

    /* While there are more integers at input */

    while( scanf("%d", &integer) <= 0)   // <--- NOTE, DON'T USE EOF!
    {
         /* Print a blank line */
         printf("\n");

         /* Update loop */
         printf("Enter integer: ");
    }

    /* convert integert to string */
    sprintf(numStr,"%d",integer);


    /* suppress msd and get weight, whatever this means */
    weight[0] = numStr[0];      // the single msd character, 
                                // copy to start of 'weight' string

    // get the weight which is the msd multiplied by the base power of 10.
    // then add a NULL character at the end to form a string

    for (i=1; i < strlen(numStr); i++)
        weight[i] = '0';   // note the difference between the zero '0' (ASCII 0x30)

    weight[i] = '\0';  // and the NULL '\0' (ASCII 0x00) characters

                                
    strcpy(suppress_msd, &numStr[1]);  // copy the rest of number 
                                       // to the 'suppresss_msd' string

    printf("\nYour number was %d.\n\"weight\" is %s, \"suppress msd\" is %s\n\n", 
                                                     integer, weight, suppress_msd);

}
jephthah 1,888 Posting Maven

sorry for the confusion.. here is the fixed version of my wild attempt at this.

Now I'm sure this isn't anything like what you need for your assignment, but it's just an example of how you could approach this without using the non-standard library functions.

#include <stdio.h>
#include <string.h
#include <stdlib.h>
//#include "presults.h"

main()
{
/* Declare variables */
int integer, valid;
char weight[3];                        // for two digits
char numStr[16], suppress_msd[16];     // oversized
char *ptr;                             // for the strtol() function


/* Prompt user for input */
printf("Enter integer: ");

/* While there are more integers at input */

while( scanf("%d", &integer) <= 0)   // <--- NOTE, DON'T USE EOF!
{
     /* Print a blank line */
     printf("\n");

     /* Update loop */
     printf("Enter integer: ");
}

/* convert integert to string */
sprintf(numStr,"%d",integer);


/* suppress msd and get weight, whatever this means */
weight[0] = numStr[0];      // the single msd character, 
                            // copy to start of 'weight' string
                            
strcpy(suppress_msd, &numStr[1]);  // copy the rest of number 
                                   // to the 'suppresss_msd' string


// you want your weight (apparently) multiplied by 10 
// so add another zero character '0' to the end.   then add a null 
// character after that to terminate the array and form a string.

weight[1] = '0';   // note the difference between the zero '0' (ASCII 0x30)
weight[2] = '\0';  // and the NULL '\0' (ASCII 0x00) characters


/* it doesnt get any simpler */
printf("\nYour number was %d.\n\"weight\" is %s, \"suppress msd\" is %s\n\n", integer, weight, …
jephthah 1,888 Posting Maven

the Apple Carbon C API generator, which this appears to be, is a Park-Miller PRNG.

note, the mod 'm' is 2^31 - 1 = 0x7FFFFFFF ... the modulus operation is being performed at the end where in teh case where mlcg >= 2^31, the mlcg is ANDed with 0x7FFFFFFF and then mlcg++ ...

that bit of trickery IS a modulus operation for Mersenne primes, the set of which 2^31 - 1 is a member

to be honest though, i'm still not certain how the iterative procedure where the top 32bits are added to the lower 32 bits (mlcg = p + q) all fits together.

jephthah 1,888 Posting Maven

the logic of your palindrome function was correct, but there were a few errors in your code structure that prevented the function from being executed properly.

. i have commented out the errors and put notes indicating what the error was.

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

 #define MAX_LEN 50

/* Create external function */
char palindrome (char *string, int len)
{
    //len = strlen(string-1);   <--- NO, do not try to take length of pointer!!
    //                               this is why "len" is passed in as an argument!
   
    
    printf("\nEntering palindrome():\n   str \"%s\", len %d\n", string, len);
 
    if (len <= 1) 
    {
       printf("   remaining length %d, all done \n", len);
       return 1;
    }
    else if (string[0] != string[--len])
    {
       printf("   str[0] \'%c\' != str[%d] \'%c\'\n", string[0], len, string[len]);
       return 0;
    }
    else 
    {
       printf("   str[0] \'%c\' == str[%d] \'%c\'\n", string[0], len, string[len]);
       return palindrome(++string, --len);
    }
}


int main(void)
{
/* Create variable for string */
    char string[MAX_LEN];
    int len;

    // char palindrome (char *string, int len);  <--- NO, do not declare prototypes 
    //                                                inside the main() function !!!


    /* Explain purpose of program to user and prompts for input */
    printf("This program will test a phrase inputted by you to see\n");
    printf("if it is a palindrome.\n\n");
    printf("Please enter a word or phrase of no more than 50 characters.\n\n>");


    /* Receive input from user and stores it in input_string array */
    scanf("%s", string);

    len = strlen(string);   // <-- DO get the length of the string after the input

    /* Call external …
tux4life commented: ++rep for all your effort :) +8
jephthah 1,888 Posting Maven

Any one have any suggestions for this i need it to output a floating point decimal and i cant seem to get it to read or print right. This class is as tough as learning to read Russian backwards.

... i want to know what the hell i am doing.

goodlord, this is an effing mess.

it's no wonder you don't know what you're doing. no one can even read this. this is an illustration of what not to do. it's called "obfuscated code" and it's not a desirable thing.

i can't help you with this if you're going to continue to use scanf(). if you want to learn how to use standard function fgets() atoi() and a few others, i'll help you clean this up.