PDA

View Full Version : I am a complete N00B at this, please help



Petoux
12th April 2006, 10:05 AM
I have never done 'C' before, and my teacher explains and teaches like shit, so I need a lot of hwlp with this, since I have no idea what to do or where to start.

I need some advice, and maybe a push forward?

Anyway, this is the 'C' (NOT C++) program I have to write:

Step 1: Pick ten (10) non - zero two (2) digit numbers.

Step 2: Sort the integers in ascending order.

Step 3: Wait

Step 4: Check and see if your two (2) digit numbers is in the "list".

Step 5: If it's found, you get a one (1).

Step 6: If it is not found, you get a zero(0).

Step 7: If you found it or not ; wait.

Step 8: Pick a different non - zero two (2) digit number.

Step 9: If you enter a zero (0) for the number you picked, you gave up.

Step 10: Stop, and terminate the program.

^That's what I have to do, and I have NO idea how to start, what to do, or ANYTHING what so ever.

Please help.

Thanks.

:D

Earendil
12th April 2006, 10:27 AM
Aha, my C knowledge will finally come in handy! I just wrote my C programming final exam yesterday, so I'll see what I can do. If you just started programming in C, I don't really see how the teacher can expect you to do this...

So, it sounds like they're giving you a list of numbers and you're entering 10 numbers yourself and it's going to check for those numbers in that list? Or are you giving it 10 numbers, sorting them, and then checking those 10 numbers for the number you enter?

I don't know how much you know, but what I'd do is something like this:

Use a loop to scanf the numbers into an array. Sort this array. You can find sorting code here (http://www.concentric.net/~ttwang/sort/sort.htm). I'd reccomend bubble sort since it's the easiest.

I'm not sure how you'd "wait"...

You could then write a basic search function, which returns 0 or 1 based on if the number is found. Usually this is something a teacher will tell you how to do...

As for entering the 0 to stop, you could always use a break statement, or a while loop for the entire thing.

Petoux
12th April 2006, 10:32 AM
I am to give it 10 numbers, sorting them in the order I mentioned, and then checking if those 10 numbers are the numbers I entered.

And, I have no idea what the hell you are talking about. Sorry to say.

How do I do a loop? I have no idea where to start like I said.

All I know, to tell you the truth, is what binary is and how to do it. That's all, nothing else.

See my problem?

:/

Earendil
12th April 2006, 10:40 AM
Hmm... That's almost an impossible task to do then.

So, you enter 10 numbers, sort, then enter another one and check if it's in that list?

If so, I can probably help. Does your teacher expect code, or just like... general ideas on how to do it? It sounds like he expects you to know C already.

Petoux
12th April 2006, 10:48 AM
I guess I don't have to wait, that was a misinterpretation. Just skip the waits, and do the rest is what my teacher said.

Then I said "But what do I do? I don't know 'C' at all period."

Then he said "You'll figure it out I'm sure."

What the hell is his problem?

Man, my teacher sucks, and EVERY other student agrees with me, in fact almost 95% of the entire class reported him to the dean for terrible teaching skills.

He still hasn't learned, and he is extremely pissed he got reported.

:/

Earendil
12th April 2006, 11:00 AM
So, basically, for having just started, this is actually fairly involved. Below is an example of a program that will do what you said. You can copy/paste it into a compiler and test it, but it works for JFE which is a fairly common on.

Here's an explanation of what's going on. The syntax and stuff is probably easily enough looked up in a textbook.

You start with #include <stdio.h> which allows you to use printf/scanf functions in your code.

In the int main, there's a bunch of integer variable declarations which we use later.

The first For loop repeatedly asks the user to enter numbers, which it saves to an array (technical name for a list). Below that, the list is sorted through the next two For loops. After that, I just stuck in one more for loop to print the numbers to show that they're now sorted.

For searching, I have a While loop, which means that it will run the code within that loop until the condition val=0 is met, meaning until the user enters 0. It then calls the function search (As seen further below). It prints a 1 if it's found, a 0 if it's not.

The search function is a basic search algorithm which should also be in a textbook.

So, yeah. This is a ridiculous question to start with. As for translation into binary code, there might be an internet site which can do that.




#include <stdio.h>

int search(int[], int);

int main ()
{
int list [10];
int i,j,val = 1;
int temp;
for (i=0;i<10;i++)
{
printf("Enter a two digit number: ");
scanf("%d",&list[i]);
}

for(i=0;i<9;i++)
{
for(j=1;j<10;j++)
{
if(list[j]<list[j-1])
{
temp = list[j];
list[j] = list[j-1];
list[j-1] = temp;
}
}
}

for(i=0;i<10;i++)
printf("%d ", list[i]);
printf("\n");

while(val!=0)
{
printf("Enter a value to search for: ");
scanf("%d",&val);
printf("%d\n",search(list,val));
}

return 0;
}

int search (int array[], int value)
{
int i = 0;
int found = 0;

while (i<10&&!found)
{
if(array[i]==value)
{
found=1;
return found;
}
i++;
}
return found;
}

Carnage
12th April 2006, 12:19 PM
2 things about that code.
1) bubble sort is one that you should never use...
2) as you ahve a sorted list, use a binary search instead of a linear one.

Xull
12th April 2006, 04:47 PM
Anyway, this is the 'C' (NOT C++) program
C & C++ are almost the same... Not much difference except the approach & classes :)

How do I do a loop? I have no idea where to start like I said.there are several types of loops...
for loops, while loops, and do loops.
for loop:

for (variable = start; condition; incrementor)
{
}
variable can be a previously declared one or declared in the same location.
for example, int i.
quick note on variable declaration: in cish code, the type of the variable precedes the variable name. so int i means "an integer named i"
start is the starting value of the variable. For example, 0
in combination with the example for the variable here, that gives

int i = 0;
The variable name can be anything you want, as long as it starts with a letter (or underscore... but don't do that), and can contain letters, numbers, and underscores.
i is a common variable used for keeping track of array indexes.

condition is any expression that evaluates to a non-zero value if it is supposed to continue looping. For example, i < 10, or i >= j.

incrementor is any statement that advances the appropriate value to the next step... for example, i++.
(++ increments the variable's contents by 1)
It doesn't have to go up by one, you could, if you wanted to, double the value, or for that matter, it doesn't even need to be a number... (in the conceptual sense, ignoring that everything is a number on the computer)

then there is the braces {}.
Braces define blocks in C/C++: groups of statements. If you don't use them, only the very next statement will be processed when looping. The braces tell the compiler to use all of the statements in that block together.

so, putting those examples together:

for (int i = 0; i < 10; i++)
{
}
it'll loop through 10 times; i will start at 0 and stop looping once it is no longer less than 10 - so 9 is the last i value for which it processes that block.

while loops are a little simpler:

while (condition)
{
}
condition is the same as before, some condition that evaluates to a nonzero value (usually true) to iterate (loop) through another time.


do
{
} while (condition);
this loop is similar to the while loop, except that it runs through the code at least once - the while loop checks to make sure the condition is true before running through it, the do loop checks afterwards.


I'd reccomend bubble sort since it's the easiest.What Carnage said. Bubble sort is very inefficient... If you want to use a simple sort as an example, I'd recommend selection sort... its similar (and also easy to learn), but isn't quite as bad as bubble sort... a bit more optimized.

As for the waits, if you are asking for input, it should wait automatically...

As for binary search, though I'd recommend using it otherwise, I'd say keep it linear for the purposes of this program (more straightforward, easier to understand... I think you have enough to figure out atm ;))


As for translation into binary code, there might be an internet site which can do that.um just compile it? I'd assume that the school has compiler(s) installed... probably g++/gcc considering its a school :P


While at it...
some C tutorials:
http://www.cprogramming.com/tutorial.html
http://cplus.about.com/od/beginnerctutoria1/l/blctut.htm
http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html

see if those help any :)

Carnage
12th April 2006, 06:41 PM
well, swordsplay mentioned binary. i thought perhaps that that might have been the whole point of sorting the list first. if not you may as well use a linear search on it from the start.

Xull
12th April 2006, 10:29 PM
The way I understood it was "The only thing I know/understand about computers/programming is binary" or something on that order... :nervous:

Petoux
13th April 2006, 11:48 AM
By Xull:

The way I understood it was "The only thing I know/understand about computers/programming is binary" or something on that order...



Yeah, true.

What I meant was, I know what 10110111 means. It's a fairly large number. I wasn't talking about a binary search. :p

The second part of what I need, is translating that 'C' program that Earendil gave me, so what the teacher expects is, to translate from 'C' to binary, it would look something similar to this:

10011011
00101100
11110110

etc..

until the program terminates.

My teacher said that each command that you give the computer translates into two binary code equivalents, so say you have 10 commands, it would translate into 20 binary (8 digits like above) in return.

00000000 means to stop/terminate the program so it doesn't go in an infinite loop, and would have to have a forced quit is what my teacher calls it.

So, it would have all that binary like above, then at the very bottom, it will have all 0's.

So, let's say that code above that was given to me has 150 commands (which it looks like it has) it would have 300 binary equivalents, and the bottom one would contain 00000000.

11111111
10001010
..
.
..
..
.
etc
00000000

That's what I need help with, I'll try those websites though.

P.S.: The code for both searches worked perfectly, now for the binary translation. :/

EDIT:

Let's use a little bit of code I got for an example:

int search(int[], int); <---this command has two binary equivalents (I don't know that for sure, since I have no idea :/)

int main () <---Same with this...
{
int list [10]; <---Same with this..
int i,j,val = 1;
int temp;
for (i=0;i<10;i++)
{
printf("Enter a two digit number: ");
scanf("%d",&list[i]);
....

return found; <--- Translates to 00000000 (I know that one at least! :p )
}

etc.

Then at the last part (which is return found; }) it would translate to be 00000000 in binary from what my teacher said, because of the reason it is the last command before/when the program terminates.

Xull
13th April 2006, 01:28 PM
The second part of what I need, is translating that 'C' program that Earendil gave me, so what the teacher expects is, to translate from 'C' to binary, it would look something similar to this:

10011011
00101100
11110110

etc..

until the program terminates.

My teacher said that each command that you give the computer translates into two binary code equivalents, so say you have 10 commands, it would translate into 20 binary (8 digits like above) in return.O.o translate C into binary?
I remember having to do that with asm, but not C right away...
Personally I'd probably just compile it and print out the binary :P


int search(int[], int); <---this command has two binary equivalents (I don't know that for sure, since I have no idea :/)that looks like a declaration... unless you mean the function call, method declarations shouldn't have a binary version (they are just a placeholder)


int list [10]; <---Same with this..That one's easy enough... type int uses 16 bits in C (as far as I can remember lol :P)
so thats just allocating 10 consecutive 2-byte sections (those 20 bytes don't have specified values - C doesn't initialize values automatically, so if you need to write that, assume 0's I guess)

int i,j,val = 1;again with the ints; i and j have no specified starting value, val does, so set that one to 1 instead.

for (i=0;i<10;i++)commands to set i to 0 can you just use registers for this :nervous:
then the comparison command (oh just have it jump after the loop code if the expression turns false)
[do the loop stuff]
increment whatever you use for i, then loop back to the condition or have another condition checker that jumps back to the top if true

printf("Enter a two digit number: ");you'll need to save the string (text & other characters) somewhere - asciiz will do.
and whatever you use to call that function...

scanf("%d",&list[i]);same thing again, only you'll need to calculate the location of the value you are referencing... address of list + i * 2 will give you that. (i = 0 for the first value)


Then at the last part (which is return found; }) it would translate to be 00000000 in binary from what my teacher said, because of the reason it is the last command before/when the program terminates.Thats rather weird... no programming before, jump straight to writing C, and converting to binary :confused:
um what platform is this on anyway? Or has your teacher assigned his/her own values for the conversions?

Carnage
18th April 2006, 02:11 PM
maybe he wants a binary, ie a compiled file.

when you compile C it compiles into machine code which is binary.

Petoux
19th April 2006, 08:10 AM
By Carnage:

maybe he wants a binary, ie a compiled file.

when you compile C it compiles into machine code which is binary.



That is correct sir! That is exactly what I need, and I have no clue how to do it.

:/

Wow, coding for the very first time sucks, especially with the teacher I got. You guys (girls? O.o) are lucky.

My teacher sucks.

:'(

Carnage
19th April 2006, 08:21 AM
windows or linux?

nevermind, we sorted this out on irc; i hope.

Petoux
19th April 2006, 05:15 PM
This is java:

import javax.swing.JOptionPane;

public class Example
{

public static void main(String[] args)
{

String scount;
int count = 1;
int inputno = 5
int sum = 0

while (inputno >= count)
{

scount = JOptionPane.showInputDialog("Enter a number " + count + " of "

+ inputno + " : ");

sum = sum + Integer.parseInt(scount);;

count++;

}

System.out.println("The sum of the input values is " + sum);

}

}



That is what I currently have for a DIFFERENT assignment that requires to do coding in the JAVA language.

By the way, I got help with that code above because I am a N00B.

:p

;)

Anyway, it says:

"Modify the program from the previous exercise (quoted above), so that it displays the sum of absolute values of a series of input numbers. That is, it should display the same result whether the input values are negative or positive.

Wtf?

:/

Why does my teacher do this shit?

>.<

Thank you all for your help, it is EXTREMELY appreciated..

:)

- Swordsplay

Seraphim
19th April 2006, 08:18 PM
Did somebody say Java? Anyway, it looks like your teacher just wants your program to add any inputted number to your sum as a positive number. For example if you entered -5 or 5 it would still add 5 to the sum regardless of the sign. If you add the Math class to your import statements you can take care of that absoulte value problem. Since the Math class is part of the java.lang package I don't think you need to import them but in case it doesn't work just put this line of code under that first line in your program.


import java.lang.Math;

Moving on... Just use the static method abs(int a) in the Math class to get their absolute value. To quote one of the lines in your program, your code would probably look something like this.


sum = sum + Math.abs(Integer.parseInt(scount));

Hope this helps a bit. If you need anything more I'm on IRC too sometimes so just leave a message and I'll find it.....eventually.

Petoux
23rd April 2006, 06:51 PM
Thanks Seraphim!!

Moving on...

This deals with the 'C' problem I mentioned earlier:

It says:




"Your "Little Man" program should then sort the integers in ascending order in memory using the selection sort algorithm.

Then convert your "Little Man" program into binarry which the output should be in an ASCII delimited text file.

Bonus: "Implement the "lookup" functionality using a binary search with your "Little Man" program instead.



Here is my "Little Man" program, I'm not sure if I even did it right. :/

Anyway, here it is:

INP 22
STO 90
INP 44
STO 91
INP 38
STO 92
INP 99
STO 93
INP 54
STO 94
INP 72
STO 95
INP 63
STO 96
INP 16
STO 97
INP 08
STO 98
INP 50
STO 99
BR0 (<--- THAT IS A ZERO, NOT AN "O") 100
OUT #1
BRU 01
LOAD O2
LOAD 04
LOAD O6
LOAD 08
LOAD 10
LOAD 12
LOAD 14
LOAD 16
LOAD 18
LOAD 20
HALT 0 <---AGAIN, A ZERO NOT AN "O")

That is where it has to translate into binary. For example, I'll take the first three:

INP -----> ????????
22 -----> 00010110
STO ---> ????????
90 ---> 01011010
INP ---> ????????
44 ---> 00101100

I got the numbers just fine, but I have to convert these into Binary:

INP = ????????
STO = ????????
BR0 (<--- that's a zero) = ????????
OUT = ????????
BRU = ????????
LOAD = ????????
HALT = ????????

That is the binary issue I am having, and I'm supposed to translate that "Little Man" program that I have into binary with the output being in an ASCII delimited text file, and as extra credit, I am to implement the "lookup" function with a binary search instead of a selection sort algorithm.

All I have to do is figure out what those things translate into binary, and to put it in that ASCII text file, and I have no idea how to do that or how/where to even start.

Everything else I have just fine, all I need (I hope *Crosses fingers*) is that.

:/

He said that if I fixed that, he said:




"If you do that, I'll give you an "A". "



Right now, I have a strong B almost a B+.

He said that 'C' code that I got wasn't part of the intended assignment, but he absolutely loved it and gave me a little extra credit, but the bulk of the assignment is what I have left.

:/

Thank you all, hopefully I can get more help.

:p

^.^

Xull
24th April 2006, 08:31 AM
Well, I couldn't find the conversions for what you use specifically... (btw thats not c, is more like a form of asm)
Anyway, your book should have some conversions for that kind of thing... have you checked the back of the book/index? There's usually an appendix somewhere for that.
(I've only worked with asm for the z80, 8080, and mips processors - all I could find about what you have was a hint that it may be based off of z80)

Either that or do you have a compiler for it/are you allowed to use it? (especially considering the output is supposed to be in ascii... conversions I had to do were to hex and binary only)

Petoux
24th April 2006, 09:43 AM
Well, I couldn't find the conversions for what you use specifically... (btw thats not c, is more like a form of asm)
Anyway, your book should have some conversions for that kind of thing... have you checked the back of the book/index? There's usually an appendix somewhere for that.
(I've only worked with asm for the z80, 8080, and mips processors - all I could find about what you have was a hint that it may be based off of z80)

Either that or do you have a compiler for it/are you allowed to use it? (especially considering the output is supposed to be in ascii... conversions I had to do were to hex and binary only)



The book doesn't have that, otherwise I would obviously do that.

:p

jk.

Anyway, about your other stuff:

Compiler for it: Nope

Are you allowed to use it? Yes, but I have NO idea how what-so-ever

Especially considering the output is supposed to be in ascii: No idea how to do that either

Conversions I had to do were to hex and binary only: Just binary for me, and the other extra credit thing.

So......

Now what?

:/

Also, thank you for all your help once again.

Sorry guys, I told you I was a N00B at this stuff.

:p

Thanks again!!

^.^

Xull
24th April 2006, 01:52 PM
ascii is just a fancy way of saying a list of bytes... basically instead of hex, you encode it to base 256.
(ascii has other implications with it, but the overall idea is the same)

Any file you save is sort of in this format... at least it can be interpreted that way.
Now I don't think that this is meant to be "saved as ascii" manually... What processor is it you are writing these programs for? I'd see if you can find a compiler for it and use it.

Petoux
26th April 2006, 10:02 AM
What processor is it you are writing these programs for? I'd see if you can find a compiler for it and use it.



This is what my teacher said about the processor:

Spark.

O.o

The computer says Sun microsystems on it if you are curious.

It also says SunRay100

Couldn't tell you anything else.

:/

Can you help with that info? Cause I have no other info for you.

:'(

Thanks.

:D

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A little update and extra info that I got too if it helps:

1.) Algorithm expressed in "Little Man Assembler"
- Create a "Little Man" program.

2.) Write a 'C' program which reads the "Little Man" code, and converts the "Little Man" code into a binary representation.

{{{Type written psuedo code ~ step-by-step approach. Algorith defined. Listed below for what I did.}}}

Convert the prgram to a "LittleMan.o" ASCII text file(s?). (Object code = .o ).

I found what these are by the way:

INP ---> 10000001
STO ---> 3 {{binary for 3}}
BR0 (<-- this is a zero) --> 7 {{binary for 7}}
OUT ---> 10000010
LOAD ---> 5 (00000101)
HALT ---> 00000000

Basically I need help with everything else.

:/

Anyway here is the pseudo code I did:

Step 1.).: Pick ten(10) non - zero two (2) digit numbers,.

Step 2.).: Sort the integers in ascending order.

Step 3.).: "Wait"

Step 4.).: Check and see if your two (2) digit numbers is in the "list".

Step 5.).: If it's found, you get a one (output a one[1]).

Step 6.).: If it is not found, you get (output) a zero (0).

Step 7.).: If you found it or not ; "Wait."

Step 8.).: Pick a different non - zero two (2) digit number.

Step 9.).: If you enter a zero (0) you gave up.

Step 10.).: Stop (terminate) the program.

~~~~~~~~~~~~~~~~~~~~~~~~~

More added info if curious:

1.) A typewritten set of pseudo-code which describes each step of your algorithmic approach.

2.) A typewritten "Little Man" program which implements the algorithm in Step 1. This file should be in an ASCII delimited text file to be processed by your Language 'C' program.

3.) A completely documented Language 'C' program which translates the "Little Man" program developed in Step 2 into the binary code equivalent. This output should be in an ASCII delimited text file.

4.) Bonus: Implement the "lookup" functionality below using a binary search.

~ Needs code assembler


~~~~~~~~~~~~~~~

This is even more info, but I doubt you need it because it wasn't really what I was supposed to do, but I will give it anyway:



#include <stdio.h>

int search(int[], int);

int main ()
{
int list [10];
int i,j,val = 1;
int temp;
for (i=0;i<10;i++)
{
printf("Enter a two digit number: ");
scanf("%d",&list[i]);
}

for(i=0;i<9;i++)
{
for(j=1;j<10;j++)
{
if(list[j]<list[j-1])
{
temp = list[j];
list[j] = list[j-1];
list[j-1] = temp;
}
}
}

for(i=0;i<10;i++)
printf("%d ", list[i]);
printf("\n");

while(val!=0)
{
printf("Enter a value to search for: ");
scanf("%d",&val);
printf("%d\n",search(list,val));
}

return 0;
}

int search (int array[], int value)
{
int i = 0;
int found = 0;

while (i<10&&!found)
{
if(array[i]==value)
{
found=1;
return found;
}
i++;
}
return found;
}




~~~~~~~~~~~~~~~~~

This is my "Little Man" converted in Binary (part 2??), but I'm not quite sure about it. Again, has to be in an ASCII text file to prove that I actually did it. :p

The directions and added info is above.

Anyway, here it is:

Before conversion:

INP 22
STO 90
INP 44
STO 91
INP 38
STO 92
INP 99
STO 93
INP 54
STO 94
INP 72
STO 95
INP 63
STO 96
INP 16
STO 97
INP 08
STO 98
INP 50
STO 99
BR0 (<---- that is a zero) 100
OUT #1
BRU 01
LOAD 02
L0AD 04
LOAD 06
LOAD 08
LOAD 10
LOAD 12
LOAD 14
LOAD 16
LOAD 18
LOAD 20
HALT 0 (<--- this is a zero)

*****************

Binary conversion:

10000001 (INP)
00010110 (22)
00000011 (STO)
01011010 (90)
10000001 (INP)
00101100 (44)
00000011 (STO)
01011011 (91)
10000001 (INP)
00100110 (38)
00000011 (STO)
01011100 (92)
10000001 (INP)
01100101 (99)
00000011 (STO)
01011101 (93)
10000001 (INP)
00110100 (54)
00000011 (STO)
01011110 (94)
10000001 (INP)
01001000 (72)
00000011 (STO)
01011111 (95)
10000001 (INP)
00111111 (63)
00000011 (STO)
01100000 (96)
10000001 (INP)
00010000 (16)
00000011 (STO)
01100001 (97)
10000001 (INP)
00001000 (8)
00000011 (ST0)
01100010 (98)
10000001 (INP)
00110010 (50)
00000011 (STO)
01100011 (99)
00000111 (BR0) <----That is a zero not an "O".
01100100 (100)
10000010 (OUT)
00000001 (1)
00000111 (BRU)
00000001 (1)
00000101 (LOAD)
00000010 (2)
00000101 (LOAD)
00000100 (4)
00000101 (LOAD)
00000110 (6)
00000101 (LOAD)
00001000 (8)
00000101 (LOAD)
00001010 (10)
00000101 (LOAD)
00001100 (12)
00000101 (LOAD)
00001110 (14)
00000101 (LOAD)
00010000 (16)
00000101 (LOAD)
00010010 (18)
00000101 (LOAD)
00010100 (20)
00000000 (HALT)
00000000 (0) <---That is a zero not an "O".

~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Again, he was kind of vague, but he said this wasn't what he was looking for, but he gave me a tiny bonus for doing it anyway.

So, with all that info, can you help?

Xull
26th April 2006, 06:51 PM
ok... so by ascii, your teacher meant the binary digits in an ascii format?

By the sounds of this

Convert the prgram to a "LittleMan.o" ASCII text file(s?). (Object code = .o ).I'd guess your teacher just meant to compile it... (".o" is typically the extension used for files you compile on solaris... at least thats the way i've seen it done)
Or was the example (assuming one) actually done by hand? or what?
considering this

~ Needs code assembleri'm assuming not...

I'm sort of getting the idea that the assignment is to write a program in C, compile it, and write another program to take that file and re-output it in binary with one digit per byte (ascii)
only this doesn't quite agree...

This file should be in an ASCII delimited text file to be processed by your Language 'C' program.
so... you have to make an assembler or something?
*sigh* someone do some brain surgery and make him more understandable? :P

and its spelled sparc, not spark ;) (pronounced the same though)

anyway, if you need to do it by hand, see if this helps:
http://www.cs.unm.edu/~maccabe/classes/341/labman/node9.html

what exactly do you need help on now though?

Petoux
26th April 2006, 07:13 PM
What I need:

1.) A typewritten set of pseudo-code which describes each step of your algorithmic approach. <--- Done, listed in my previous post. Don't need anything more on that.

2.) A typewritten "Little Man" program which implements the algorithm in Step 1. This file should be in an ASCII delimited text file to be processed by your Language 'C' program. <--- No idea how to do this. I think the 'C' program is the one listed at the bottom, but I don't think that is the case. So what I assume it really means is that the "Little Man" thing (that's above in my previous post that I gave) needs to be in an ASCII delimited text file. It is to be processed by a (my?) Language 'C' program.

Maybe create a 'C' program to do that "Little Man" thing I listed above in an ASCII delimited text file? Again, he was so vague. I listed everything he said and added already, he won't give me anymore info than that.

:/

Maybe it is more specific in step three:

3.) A completely documented Language 'C' program which translates the "Little Man" program developed in Step 2 into the binary code equivalent. This output should be in an ASCII delimited text file.

That's what I need. I already have the binary equivalent above, but I need to make a 'C' program that translates it in an ASCII text file, both of them have to be in that ASCII thingy. So, I think he means is to make a 'C' program that translates the binary "Little Man" equivalent that I posted in my last post.

Again, so vague. I hope this helps.

:/

4.) Bonus: Implement the "lookup" functionality below using a binary search.

^ Don't techniquelly need that, but it would be nice. :p

***************************************

ok... so by ascii, your teacher meant the binary digits in an ascii format?

Yes, I believe it means that. I think it states that in the 1 , 2, and 3 (maybe 4?) steps.

I'd guess your teacher just meant to compile it... (".o" is typically the extension used for files you compile on solaris... at least thats the way i've seen it done)
Or was the example (assuming one) actually done by hand? or what?

No, I don't think it was done by hand, and there was no examples at all. You are probably right with the (".o" is typically the extension used for files....etc comment you made. I guess I'll go with what you say, because it makes sense. So help with that too then?

:/

I'm sort of getting the idea that the assignment is to write a program in C, compile it, and write another program to take that file and re-output it in binary with one digit per byte (ascii)
only this doesn't quite agree...

I did a 'C' program (posted above again), unless you mean a whole entire different one.

But, we'll go on what you continued to say...

so... you have to make an assembler or something?

An assemebler sounds right, yeah. I'll go with that one.

*sigh* someone do some brain surgery and make him more understandable? :P

Nope.

:/

I'm a 100% N00B.

:p

and its spelled sparc, not spark (pronounced the same though)

LOL!

anyway, if you need to do it by hand, see if this helps:
http://www.cs.unm.edu/~maccabe/clas...bman/node9.html

Sure.... I'll try that.

what exactly do you need help on now though?

I posted it in this post, and possibly the previous post.

Help you at all?

I'm guessing not huh?

:/

Man, this really sucks.

:'(

Xull
27th April 2006, 05:45 AM
I suppose i'll just ask exactly what i think is what you are supposed to do :tongue:
hopefully we'll be on the same page then :P


A typewritten "Little Man" program which implements the algorithm in Step 1. This file should be in an ASCII delimited text file to be processed by your Language 'C' program.what format is your little man program?

INP 22
STO 90
INP 44
STO 91
INP 38
something like that?

then your program is supposed to take each line and output the binary (with each digit its own ascii character)
basically
if it reads

INP 22
it outputs
10000001 (INP)
00010110 (22)

Thats the only thing thats left that makes sense :P

I remember doing something like that for some other language, just never had to make an actual assembler (in class).

Well since that sort of makes sense, i'll just go ahead with the explanations :P
couldn't your teacher have just specified the assignment is to make an assembler?

by the looks of it, the simple default tokenizer will work just fine...
anyway, cheap way to do it would be just read in each token and check if its a command or a number...
basically


if (!strcmp(token, "INP")
fprintf("%s\n", "10000001");
else if (!strcmp(token, "LOAD"))
fprintf("%s\n", "00000101");
...
else
fprintf("%s\n", binary(token));
token is the read string (up to the space/new line), "..." would be the else if section repeated for each other command, binary() would be your function to convert the number to binary

you'd probably have to convert the token to integer form first though...
so sscanf might come in handy

sscanf(token, "%d", d);
where d would be the name of your int variable.

so... rewritten else code:


else
{
int d;
sscanf (token, "%d", d);
fprintf("%s\n", binary(d));
}

its fairly easy to find a C program to convert from int to binary in ascii format, a simple search should give several results ;)

oh um side note.... if you'd like it to have the original number in parenthesis, you could do this:

fprintf("%s (%d)\n", binary(d), d);
if you'd like it to put it all in the file without any spacing, just remove the escaped new line "\n"... though if your teacher is actually going to check what was wrong and not just that it was wrong doing this wouldn't be too helpful :P
(I'd suggest asking your teacher how exactly the output file should be formatted if you don't know/aren't sure)
(for information on the format part of fprintf and similar functions, Wikipedia (http://en.wikipedia.org/wiki/Fprintf) has a pretty good entry)

one more thing about this...
If you're writing an assembler, it probably should use the correct number of bits for each number, so you might want to ensure your binary function does that ;)

anyway, this method should do what you'd like it to do... but it doesn't really care about mistakes in the file :P

if there is an unidentified token, it'll try to parse it as a number
say you have

awefawef
since its none of the commands, it'll try to read it as a number :P
so... you might want to check about how whatever function you're using to convert to decimal handles invalid numbers (such as the sscanf example I gave)
if something does go wrong, handle it appropriately :)
and if there is a number where there shouldn't be one...
such as

INP 0 0
it would put the inp binary equivalent, then a 0, then another 0...

you could counteract this one by inputting the following token(s) as well, so if you read an inp, you immediately also read the number following it. (and again, check that it actually is a number)
otherwise...

INP HALT could cause some problems :tongue:


Also, i'm assuming that you don't have to handle comments and other data in the file... :nervous: