jephthah 1,888 Posting Maven

then it sounds more like a vista problem and less like a C problem, to me.

i don't use vista, so i can't help you.

maybe someone else knows.

jephthah 1,888 Posting Maven

well, i guess that's a trick to squeeze a couple more out.

but if you're going to try and do a "real" factorial program (not just this intro to functions), you'll need to use one of the known prime number factorization programs.

no point in reinventing the wheel.

jephthah 1,888 Posting Maven

was the C program compiled on Vista, or did you grab an already-compiled .EXE from somewhere else?

you need to have compiled the executable on Vista.

so yes, you'll need a C-compiler. MS provides a basic, free version of Visual C/C++

jephthah 1,888 Posting Maven

solutions involve using either

#include <conio.h>

or

#include <windows.h>

neither of which are portable. for Linux/Solaris, you would use the

#include <ncurses.h>

library

http://www.blurtit.com/q616731.html
http://www.daniweb.com/forums/thread34554.html
http://www.unix.com/high-level-programming/33023-blinking-text.html

jephthah 1,888 Posting Maven

I just thought i'd point out this nugget of wisdom

I'm not sure how to combine the use of bubble sorting and c I/O functions. After opening the file, I don't know how to read the contents.

Do you have a book? if you don't have a book, you need to get one.

jephthah 1,888 Posting Maven

it does work in 2's complement. i hope i didn't say something to indicate it doesn't.

consider a 4 bit number. if it's signed the range is from -(2^3) to (2^3)-1, that is, from -8 to +7 including 0. that is a total of 16 (2^4) unique numbers. if it's UNsigned, the range is from (0) to (2^4)-1, that is, from 0 to 15. that is still a total of 16 (2^4) unique numbers. apply this concept to any 'n'-bit number. so for the 64-bit number (long long) you'll still only have 2^64 unique numbers regardless if it's unsigned or signed.

as for larger factorials, i suppose you could use "double". it will give you positive range up to about 1x10E+37, with 10 digits of precision. that will give you the (approximate) value of up to 33! ... if you wanted to get fancy, you could test the input and use "long long" for values < 21 and "double" for values above that.

integers larger than 2^64 will require the use of large number caches and advanced programming beyond what your class (or myself!) should attempt to get into :P

jephthah 1,888 Posting Maven

ok, sure. you're the boss.

:)

jephthah 1,888 Posting Maven

im just saying theres a difference between asking specific questions and dumping 1000 lines of assembly code and saying "hay guys, whats this do?"

jephthah 1,888 Posting Maven

yes... sort of. using "unsigned" increases the range of positive integers by a power of 2, because it removes the need to handle negatives.

so, for "unsigned long long", you'd have the range from (0) to (2^64) - 1 ... instead of the range -(2^63) to (2^63)-1 that you'd have for just "long long" ... but it would not give you 2^128 numbers... you'll still only have 2^64 unique numbers available, whether you use "unsigned" or not.

what i was saying is that the difference between 20! and 21! is too great for the additional range supplied by "unsigned" to be of any help, in this case.

jephthah 1,888 Posting Maven

thats fine walt. you can win the point game, and you can continue with your smug self-righteousness while berating new people for asking question.

jephthah 1,888 Posting Maven

spec80, ignore walt. He's apparently got some hostility issues with people who have honest questions. nice quality for a moderator.

anyhow, don't panic. you're 99% done.

instead of locally calling "rootone" and "roottwo" inside your subroutine, use the global variables that you already declared outside of main() ... "root1" and "root2"

and since you're going to use them as global variables (which is the case, because you declared them outside of main), there's no need to declare them as pointers. just make them straight-up type doubles.

then in the subroutine, instead of assigning the results of your calculations to "rootone" and "roottwo", assign them directly to the globals "root1" and "root2".

when you return back to the main( ) routine, the roots will still be in those global variables, ready for you to do whatever you want with them ... how nice, isn't it! aren't globals wonderful? enjoy them now, while you can.... they'll be violently beaten from you soon enough!

one other thing. theres no reason to make your "compute_roots" subroutine declared as a type "double". you're only returning a 1 or a 0... declare it as an "int", or "unsigned"

WaltP commented: Don't start a flame with me. They were correct observations. -2
jephthah 1,888 Posting Maven

well, i'm sure glad you told us it was for "academic purposes". i was getting ready to call Chris Hansen

jephthah 1,888 Posting Maven

never mind about posting the 'sizeof' vars back here. it's definitely your code that is causing the problem, not the compiler. i just wrote a program similar to yours that works. i could just post it i suppose, but that wouldn't help you out much.

the main problem is that your 'num' is declared (in both cases) as an int, yet you're trying to return one instance of it back through through the factorial function as an unsigned long long.

redeclare num as a 64-bit long. you might want to give one of the 'num's a different name just for clarity... it gets confusing when you have different variables in different functions that use the same name. confusing for the reader (and programmer!)... compiler doesnt care.

interesting though, how yours turned out, it actually gives the correct answer for 13! = 6227020800, which is clearly greater than a 32-bit number. it definitely does break down after that.

FYI, you dont really need to do "unsigned long long"... "long long" will suffice -- both of them happen to cross the 64-bit boundary at 21!, so neither of them work past 20!

jephthah 1,888 Posting Maven

compile the following program, run it, and print the results back here

#include <stdio.h>
int main()
{
  printf("sizeof(char) == %d\n", sizeof(char));
  printf("sizeof(short) == %d\n", sizeof(short));
  printf("sizeof(int) == %d\n", sizeof(int));
  printf("sizeof(long) == %d\n", sizeof(long));
  printf("sizeof(long long) == %d\n", sizeof(long long));

  return 0;
}

this will show you that 12! is good for longs, but you do indeed need a long long for 13! and greater.

meanwhile the real problem is in your type casting.

.

jephthah 1,888 Posting Maven

god forbid someone ask a legitimate question here without getting told to RTFM. i see now how Walt has so many 'solved' threads.

one problem is youre putting your filenames in quotes. otherwise, its pretty simple, just use "fopen" and "fgets" in a this manner.

printf("enter filename: ");    
    fgets(filename, 120, stdin);
    filename[strlen(filename) - 1] = 0;

    if ( (fptr = fopen (filename, "r")) != NULL)
    {
         while (fgets (buffer, 30, fptr) != NULL)
         {
              buffer[strlen(buffer) - 1] = 0;
            
              <etc...>

at which point you will now have the first line from your file in the string "buffer', and notice that the newline is being replaced by a NULL character on line 9

some assumptions are being made ... importantly, that there will never be a single line in your file that is larger than 30 chars in length.

if you need to read longer lines, change the argument in fgets (and size 'buffer') accordingly

jephthah 1,888 Posting Maven

bah. fine, you can have it


:P

jephthah 1,888 Posting Maven

when i say "readable" i meant for the average programmer. not for someone who can't grasp that "!" is the same as "== 0 "

perhaps we should

#define YES_INDEED_THE_STRINGS_ARE_EQUAL            0

?

jephthah 1,888 Posting Maven

gah. you're right. he is a sadist.

i'm going to apologetically back out of this now. I'm too far removed from assembly these days to be of any help. its been 5 years since i took it in school.

hopefully someone else can help you here. personally, I'd be hitting his office hours or his TA.

jephthah 1,888 Posting Maven

i'm sorry, i totally misunderstood the question. forget everything i wrote. this is not that unusual after all.

do you have any more information about this problem?

jephthah 1,888 Posting Maven

good point.

fflush is worthless in this. i should have removed it.

jephthah 1,888 Posting Maven

at least some of your problem may be due to fgets( ) retains the newline character.

but im not sure how some of the other stuff you're doing would work... anyhow, I think you'll find it's best to try to keep code basic and readable for the next guy, rather than using clever logical constructions.

FWIW, my own style is that i don't like "do/while" loops when a simple "while" will suffice.

while (1)
{ 	
    printf("\nDo you wish to continue ('yes' or 'no'): ");		
    fgets(progress, sizeof progress, stdin);
    fflush(stdin);

    for (a=0; a<strlen(progress)-1; a++)
        progress[a] = toupper(progress[a]);
    progress[a] = 0;				

    if (!strcmp(progress,"YES") || !strcmp(progress,"NO"))
        break;			

    printf("\nOnly 'yes' or 'no' will be accepted!\n");	
}
jephthah 1,888 Posting Maven

youre crazy if you think anyone is going to try and read that mess.

you need to formulate some kind of a coherent question about what you're trying to do with this guy's code.

jephthah 1,888 Posting Maven

this is about the weirdest homework question ive ever come across..

XOR is the only two-way function so could only be the possible answer.

but thats a ridiculous way to handle addressing. it requires that one half basically be constant.

jephthah 1,888 Posting Maven

"I created a text box ..."'

you created a GUI and you're trying to get the input to a file handle?

jephthah 1,888 Posting Maven

o rly?

jephthah 1,888 Posting Maven

use the perl command

utime( )

to change the timestamp of the file to be whatever you want.

http://www.perl.com/doc/manual/html/pod/perlfunc/utime.html

jephthah 1,888 Posting Maven

and here's a bunch more:

=     +=     -=     *=     /=     %=     <<=     >>=     &=     ^=     |=

"a *= 4 " is the same as "a = a * 4"
"b <<= 1" is the same as "b = b << 1".

in other words, multiply 'a' by 4 and put the result back in 'a'. ... shift 'b' left by one bit, and put the result back into 'b'.

one main thing to be aware of is "operator precedence". any statement that has more than one operator, the order of operations is strictly defined. so in C/C++, the following two expressions are the same:

ans = a + b * c;

ans = a + ( b * c );

but are completely different than

ans = ( a + b ) * c;

you can find a list of "operator precedence" most anywhere

http://www.difranco.net/cop2220/op-prec.htm


.

jephthah 1,888 Posting Maven

great! apologies if i was being overly explanatory.

:)

jephthah 1,888 Posting Maven

glad to help :)

jephthah 1,888 Posting Maven

I'm asuming you already understand the relationship between hexadecimal(base16), decimal(base10), and binary (base2), and that any integer value can be represented simultaneously by either of these systems.

LINE 8: the hexadecimal value "8000" in binary looks like this "1000 0000 0000 0000"

it is the "bit mask" that checks the highest (most significant) bit in the value being tested.

the check is accomplished by using the "bitwise AND" operator: "&". it performs an AND function on each and every bit in the value against the 0x8000 bitmask. look up "bitwise AND" if you dont understand this.

LINE 12: this shifts the value being tested one bit to the left, causing the next highest bit to become the MSB (most significant bit) and thus be the one that is checked by the bitmask in LINE 8.

this is accomplished by using the "logical shift left" operator: "<<" it shifts each bit one place to the left, dropping the MSB off the left end (lost forever), and padding 0's into the empty LSB spot on the right.

you should use your IDE (development environment) debugging functionality to step through this program and watch the values change in binary and/or hex, along with the string value that contains all '1' and '0' characters to represent the binary value.

jephthah 1,888 Posting Maven

heres how this works: FIRST, you write your program, or at least part of your program. THEN, when it doesn't work, you come here and ask why doesn't such-and-such work, and show what you were trying to do. FINALLY, we will show you how to fix it and make it work.

here's how this *doesn't* work: you come here and ask people to write your program for you.

now you better get busy, time is running out.


EDIT: Okay, here's some hints

you have to declare your array variable like so:

int myArray[3][3];

where the first subscript is the row, and the second is the column. they will be indexed using the numbers 0, 1, and 2.

to add the three values in the first column together, you could do it like this:

temp_answer = myArray[0][0] + myArray[1][0] + myArray[2][0];

but rather than hard coding the script indices, you should rather make use of "for" loops.

jephthah 1,888 Posting Maven

what he said ^, and also, your function

show_costs

should have the conditional "count<5". otherwise the index [4] never gets set

jephthah 1,888 Posting Maven

strtok( ) is a good function for this.

you tokenize each word based on "whitespace"

so

single_word = strtok(MyBuffer," ")

is the first call and it returns to "single_word" the pointer to a string that is everything up to (and not including) the first whitespace. in other words, it points to the first single word found in "MyBuffer".

subsequent calls to strtok( ) use the NULL argument to indicate that it's a continuation of the original handle (started with "MyBuffer").

single_word = strtok(NULL," ");

and each time this is called (typically from a single loop) it will keep putting the next "word" into the string pointed to by 'single_word'

once there are no more "words", buffer will be the NULL pointer, so you need to test for this each time.

while (strtok(NULL," ") != NULL)
{
<stuff to do>
}

caveats: "MyBuffer". is often read from a file one line at a time... words at the end of the line are not necessarily entire words. they may be chopped and continue on the next line, and thus in another buffer. special handling will be needed for these situations.

another thing to consider is that the original string (what i called "MyBuffer". above) will be beat up by the use of strtok( ). specifically the actual text in the buffer will have each of the tokens (in this example the whitespace
" ") replaced with a …

jephthah 1,888 Posting Maven

do you mean you don't know how (or where) to declare the variable, "A"?

to make a long story short, for now you can just try declaring it as a global variable (outside the "main( )" routine) like so:

int A[MAX_ROWS][MAX_COLS];

where MAX_ROWS and MAX_COLS can be #define'd or simply hard coded as a direct number ("int A[30][30]" for example...). of course if your user tries to define a matrix with more rows/columns than you've allowed, the program will crash.

whenever you want to return an entire array (matrix), you would just

return A;

but in this case, you won't actually return it. because the variable is being held globally so theres no need to return it. its values will just "be there" . in this case you would just "return 0". You could also conditionally return a non-zero number to differentiate whether or not the function "passed" or "failed", if you want to get fancy...

but another problem this exposes, is that you don't want to pass those row and column variables to the function from the caller. just declare them locally, inside the SetMatrixA function itself. so now the function call will look like this

int SetMatrixA(void) {
     int j, k, r1, c1;

     <rest of function>

declaring the matrix "A" as a global variable is not the best way to go about this, but the way it should be done involves pointers, and we can save that for another time. this …

jephthah 1,888 Posting Maven

like Dragon said, you cant nest subroutines inside main, as you're doing. each function (including main) has to each be separate from each other.

other than that, the problem you've identified is that you're trying to treat a single char variable ("menu") as an array of chars. i think the confusion is simply that you've made a typo and called the wrong variable in your printf() statement. the "menu_item" array is the one i think you meant to call.

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

should be

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

still you need to fix the nesting issues as mentioned. it wont ever compile the way it currently is arranged.

jephthah 1,888 Posting Maven

i feel like i have touched history.

jephthah 1,888 Posting Maven

it's not being skipped. it's running, and doing exactly what you've told it to do. which, unfortunately, is not what you intended it to do.

every time the "count" loop in main() is executed, it calls your subroutine. every time the subroutine is called, it sets "scrub = 0". scrub is always less than 5, so the first condition is always executed. the second condition will never occur.

you need to make "scrub" a variable declared in "main()" and have it passed as a pointer to the subroutine. assuming that you want scrub to be incremented, as it appears.

there are other ways to do it, of course, such as declaring 'scrub' global in scope (outside main). But i don't recommend that option ... although others may recommend it just as a "get-r-dun" sort of thing.

jephthah 1,888 Posting Maven

nm

jephthah 1,888 Posting Maven

The problem is people who don't realize that scanf is designed for strictly formatted input.

Okay. I'll buy that. nice code, by the way

jephthah 1,888 Posting Maven

>"don't ever use scanf() in the real world" ...
Without any error checking.

are we still here?

look, in the real world (i work for a medical device manufacturer), I would be run out of the plant if i tried to pass a program to the assembly line that used

scanf("%f",&myFloat);

please put all the "error checking" you like and then tell me how the input:

$12.34

works out for you.

jephthah 1,888 Posting Maven

apparently a second decimal will not crash/hang the program

if you put a valid value in scanf, followed by something invalid, it will accept the valid part and ignore the invalid part. but if you start with something invalid, it will go haywire

12.34 valid part = 12.34
12.34.5 valid part = 12.34
12xz99 valid part = 12 (12.00)
12..34 valid part = 12. (12.00)
$12.34 invalid -> program goes out of control.

so we can argue the details, but the point i'd still say is "don't ever use scanf() in the real world" ... however, if your instructor is okay with you using it, then enjoy it while you can :)

another couple things i see with your code:

the line

return();

is invalid... you don't return a void. you also only use () if you need to evaluate an expression before returning the value.

and the line

scanf("%c", &cResponse);

is doomed to fail. consider getc() or fgets() instead

jephthah 1,888 Posting Maven

did you get it to work, friendfx?

jephthah 1,888 Posting Maven

Obama needs to select a running mate that will help with the "weak on defense" perception.

I'm thinking Richard Perle.

jephthah 1,888 Posting Maven

Please point out said snarky witticisms and thinly-veiled ad hominems; I'm having trouble seeing them in his writing.

read his responses in his two most-recent posts.

if you cant comprehend them, i can't help you.

jephthah 1,888 Posting Maven

Ezzeral, unfortunately Sinkula thinks we're all so stupid that he can pass snarky witticisms and thinly-veiled ad hominems off as meaningful answers.

Dave Sinkula commented: I you have nothing to say, please don't pass up the opportunity to say nothing. -2
joshSCH commented: All you've done in this thread is attack people. Get lost. -2
jephthah 1,888 Posting Maven

never, ever, use GOTO

a horrible horrible thing they did, by allowing that command in the language.

use a while() loop instead.

jephthah 1,888 Posting Maven

Yes, I was kidding. Keep posting, and don't take anything personal.

i know, i will, i dont

:)

now, back to the OP

jephthah 1,888 Posting Maven

Ezzeral, don't feed the troll.

jephthah 1,888 Posting Maven

So far the judges are not impressed.

what? are you kidding?

i even had to go look up "tendentious", dammit.

:P

jephthah 1,888 Posting Maven

okay, okay. I used the word "crash" imprecisely. An "infinite loop" is not technically a "crash".

but when i have to hit Control-C or CTRL-ALT-DEL to stop an out of control program... my engineering managers and VP of production will want to know why my crappy program "crashed" the entire production line and put 30 assembly workers off their tasks.

they will not care to investigate rhetorical turns of phrase as to what, exactly, "crash" means.

still, you do get the point for precision.

gg