[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Can someone point me in the right direction here? I've got
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 85
Thread images: 8
File: garbage.png (37 KB, 676x252) Image search: [Google]
garbage.png
37 KB, 676x252
Can someone point me in the right direction here? I've got all my code don’t except for the algorithm to choose whether each number is larger, smaller, or equal to its neighbour. I know this is retard level stuff but nothing I've tried has worked perfectly.
>>
>No logical or relational operators allowed
wew
>>
what the fuck m8
>>
File: other garbage.png (40 KB, 687x255) Image search: [Google]
other garbage.png
40 KB, 687x255
>>53088700
Yeah, we're just getting to that in lecture this week. This was the last assignment, which was a lot easier, but same story, only we couldn't use functions outside of main.
>>
>>53088541
It says no logical operators, but not arithmetic.

Just loop those numbers and order them.
>>
>>53088798
Can I please see the code that you did for this, assuming you did it?

>>53088813
https://en.wikipedia.org/wiki/Relational_operator
>>
>>53088798

surely saying you can't use "relational operators" is an error.

do you cover conditionals (if else) in the first 4 chapters of the book? and do they use > and < symbols?

if not i'm genuinely curious about what you're supposed to be doing here.
>>
>>53088845
no if else, no loops, nothing. you're supposed to accept 5 integers from a user, then print them out, along with a >, <, or = sign between them signifying their value relative to their neighbours.
>>
File: bsshit.png (25 KB, 442x535) Image search: [Google]
bsshit.png
25 KB, 442x535
>>53088837
>>
What the fuck does this teach students?

What are the names of the first 4 chapters?


What language? C/C++?

Just throw some asm in there lol
>>
What does chapter 4 cover?
>>
>>53088940
its a garbage freshman weedout class for engineers. it's C
>>53089008
user-defined functions
>>
>>53088541
Those exercises are stupid. And the teachers too.
>>
only thing I can think of is massive quantities of "switch"
>>
>>53088938
Isn't your assignment more of that same shit?
Just throw that shit in a function and operate on the last two integers entered.
>>
I basically just need some function that returns a -1, 0, or 1 based on whether the first value is less than, equal to, or greater than the second value
>>
>>53089048

(assuming "integers" means "integers under 10" that is)
>>
>>53089051
>Isn't your assignment more of that same shit?
yeah, but instead of returning the larger value, i need >>53089057. it would be easy if you didnt have to worry about any values being equal to one another
>>
Can you use arrays (or strings since they're just char arrays) in this? Something like ['=', '>', '<']

Then you could use the values from the arithmetic operations as an index to print the correct char out
>>
It looks like it work for non negative integers.

char op (int a, int b) {
if (a - b) {
if ((a % b) - a) {
return '>';
} else {
return '<';
}
} else {
return '=';
}
}
>>
>>53089228
Read the assignment, it's very restrictive.
>>
>>53089244
What rule did I violate?
>>
>>53089251
No ifs, for example. It's not covered in the first four chapters according to the OP.
>>
>>53089273
It becomes tricky.
>>
>>53089251
OP here, thanks for the help, but the solution cannot use any loops, if else statements, etc.
You need some sort of expression that will yield the values for the characters = < or > and print them between each integer
>>53089215
yes, which is why i'm trying to find >>53089057 and then add 61 to it, or whatever it would be to get < > or =
>>
>>53089057
((x >> 31) * -1) + (((x-1) >> 31) * -1) + 1


Assuming 32 bit integers, the first summand is -1 if x<0, and the second is -1 if x<=0, else 0
>>
>>53089352
Let's hope the OP learnt about bitwise shit in his first four chapters.
>>
>>53089352
>Assuming 32 bit integers
That's not C specs.
>>
>>53089352
I don't think I can do that.
>>
>>53089444
Are them integers mandatorily positive or are you allowed to input negative integers?
>>
>>53089465
all input will be non-negative
>>
>>53089487
Then can't we do something with the result of e.g. (int1 - int2) / (int1 + int2)?
>>
#include <stdio.h>

int main (void) {
for (;;) {
int a;
int b;
int c;
int d;
scanf ("%d%d", &a, &b);
c = (a - a % b) / a;
d = (b - b % a) / b;
printf ("%d %d => %c\r\n", a, b, "<>="[2 * c + d - 1]);
}
return 0;
}


I stop there, it's too stupid.
>>
>>53089533
isnt that always 0? and undefined when both values are 0
>>
>>53089609
You're right.
>>
File: 1455952655661.jpg (64 KB, 491x713) Image search: [Google]
1455952655661.jpg
64 KB, 491x713
>>53088541
>this shit is supposed to teach people programming
>>
>>53089544
No selection constructs allowed.

>>53089533
This works with floating point division and if there's a way to get -1 from a negative float and 1 from a positive float.
>>
>>53089662
>No selection constructs
What is it?
>>
>>53089672
Ifs and loops for example. But I'm retarded, the loop in that code is just to get the input so it's fine.
>>
Well, OP? Did >>53089544's solution work for you?
>>
>>53089544
thanks, appreciate the help
>>53089862
kinda, but i'm getting a floating exception because for some reason, when i get the integers from the user, they're correct, but when I pass them to the function that is supposed to determine their relative sizes to their neighbors, the int values are all fucked up. idk why its not passing the correct values
>>
File: Amgv8tI.png (6 KB, 401x169) Image search: [Google]
Amgv8tI.png
6 KB, 401x169
>>53089544
Not OP.

This doesn't works if one of the inputs is zero, because division by zero problems.

Also, I tried to implement this (in ruby, because I'm a pleb) but I'm getting weird results depending on the number, as in pic related.
>>
>>53090563 here. This is the code of my Ruby implementation. I believe it should work the same as the C program.

arr = ['<', '>', '='];
while true
print("Enter the five integer values: ");
str = gets.chomp.split.map(&:to_i);

for i in 0..3
c = (str[i]-str[i+1])/str[i];
d = (str[i+1]-str[i])/str[i+1];

print("#{str[i]} #{arr[2*c+d-1]} ");
end;
puts(str[i+1]);
end;
>>
>>53088541
>create an array with elements ['<', '=', '>']
>subtract numbers
>bitwise AND the first and last bits of the result so it's either -1 0 or 1
>return the array element at index (result+1)
>>
why are the correct values not being passed to my convert function? top values were printed after they were scanned, bottom values were printed after being passed to a function to compare the numbers against each other. so i'm getting an floating exception because my values are being passed incorrectly
>>
File: trash.png (2 KB, 255x41) Image search: [Google]
trash.png
2 KB, 255x41
>>53090834
>>
File: wut.png (23 KB, 474x1203) Image search: [Google]
wut.png
23 KB, 474x1203
>>53090859
what exactly am i doing wrong here that i'm not able to pass the correct values? i'm borderline retarded when it comes to programming, when ironically enough my dad is a programmer. not to mention this class is fucking awful at teaching you anything
>>
>>53091050
You're not necessarily bad at programming. You're having trouble with an extremely restricted kind of programming that forbids you from logically and clearly expressing what you want to do. This kind of programming might be useful for some kind of very performance critical hardware programming (which could be useful since you said it's a class for engineers). But it's completely different from normal programming.
>>
>>53091268
nah, it's not just this, I had a project for a grad level course in fuzzy logic where we developed a predictive/adaptive cruise control system with collision detection and avoidance in MATLAB and i felt lost. I was the guy with the ideas and i came up with the formulas and relevant data and literature, but I was lucky to have a grad student who did a dual degree in his undergrad in EE and computer engineering, so he did all the programming. im not really sure why but something about it just doesnt click with me. like i understood everything we were doing and how it worked as a system, but i didnt really know how to properly implement it in code. i'd never really been introduced to object oriented programming though. idk it's not my cup of tea
>>
>>53091050
to do what you're wanting to do you will have to define your int val's as globals. They become out of scope for functions. Or you can make your vals an int array and pass it around by reference.
>>
>>53088938
>math.h
>>
>>53091520
we're not allowed to use global declarations in the class. how to make int array? we havn't learned that either but if i get penalized for it, it would be a lot less than not submitting it or using globally defined variables. and when you say pass by reference do you mean pass by address? i'm familiar with that phrase, not the former
>>53091559
i always include it just in case, even though i think the assignment explicitly says its not needed lel
>>
>>53091050
I think this has to do with variable scopes, OP.
To your code, the val1 declared in main() and the val1 declared in
valRequest()
are two different things. When you change the variable values with scanf, you're only changing the
valRequest
variables.

There are two ways of solving this:
1) You can do the variable reading in main(), which I highly recommend.
2) You can pass the variables to valRequest by reference. I'm not sure if this has been explained to you yet, and I'm not a C programmer, but basically when you send a parameter to a function, you're just sending the value that's inside the variable (this may change depending on the programming language but it's how it works in C).
So the way to get around this is to send the variable's memory address to the function as a parameter (with the & operator), and then inside the function you say "hey, let's go to the value that's inside this address and fiddle with it" (with the * operator).

In your code, it should go like this:
#include <stdio.h>

int main() {
int var1;
int var2;

printf("%d %d\n", var1, var2);
valRequest(&var1, &var2); // Passing the addresses to valRequest
printf("%d %d", var1, var2);
}
// Here we're dereferencing the pointers
int valRequest(int *var1, int *var2) {
scanf("%d %d", var1, var2);
return 0;
}


Pointers are quite an advanced topic in C though, so don't get too worked up if you can't understand it just yet. I'm only demonstrating it in case your teacher has already gone through this.

Further readings on scope:
https://en.wikipedia.org/wiki/Scope_%28computer_science%29
http://www.tutorialspoint.com/cprogramming/c_scope_rules.htm

And on passing by reference:
http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value
http://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm

Hope it helps.
>>
>>53091583
Damn that turned out to be longer than I thought.
Oh and also I forgot that you can declare them as globals, like >>53091520 said. Probably is the simplest way out.
>>
>>53090745
>Last bit will always be one
>>
>>53091614
thank you. we just learned pass by address last week, and i forgot since i've been working on my senior design project.
>>
>>53091583
do i need to do the same thing with the values i get in my comparison function that I pass to the final print statement?
>>
>>53092239
Yep, if you're still using the same code from the image you sent on >>53091050. But you'll only need the * operator on the 'comp' variables, since you're not changing the value of any 'val' variable anymore.

A better approach, IMHO, would be to make
compare
return the string already built to the main method, something like
u + ' ' + comp12 + ' ' + w...
. Not sure if that works in C though, since IIRC there are some memory allocation problems with strings.
>>
>>53088541
Can you use arrays?
int sign(int x)
{
return ((unsigned)-x >> 31) - ((unsigned)x >> 31);
}

main() {
a = []char{'<', '=', '>'}
int v1,v2,v3,v4,v5;
scanf("%d %d %d %d %d", &v1, &v2, &v3, &v4, &v5)
printf("%d ", v1)
printf("%c %d", a[sign(v1 - v2)], v2)
printf("%c %d", a[sign(v2 - v3)], v3)
printf("%c %d", a[sign(v3 - v4)], v4)
printf("%c %d", a[sign(v4 - v5)], v5)
}
>>
>>53092978
Nevermind, saw you can't use bitwise operators. This is interesting.
>>
>>53093001
>This is interesting
no it's not, it's dumb as shit and meaninglessly restrictive.
>>
so what was covered in the first 4 chapters that you're only allowed to use?
>>
>>53093040
this, give us the title and edition of the book you are using. if it's professor notes then link us.
>>
>>53093040
Chapter 1: Introduction to Computers
Chapter 2: Introduction to the C Language
Chapter 3: Structure of a C Program
Chapter 4: Functions

basically just arithmetic, not loops, selection, no if/else, we get jack shit to work with

>>53093052
The book is B. A. Forouzan, R. F. Gilberg "Computer Science A Structured Programming Approach Using C"
>>
>>53093092
third edition
>>
>>53092978
Believe this gets the same results without bitwise operations:

int sign(int x) {
return ((unsigned)-x / 2147483648) - ((unsigned)x / 2147483648);
}
>>
File: wut.png (46 KB, 730x1171) Image search: [Google]
wut.png
46 KB, 730x1171
here's everything i have so far, and the output. printing the int values of comp because i wanted to see what they were and why the printing the char got me nothing. appreciate the help
>>
>>53093298
So the full program is:

#include <stdio.h>

int sign(int x)
{
return ((unsigned)-x / 2147483648) - ((unsigned)x / 2147483648);
}

int main()
{
char a[] = {'<', '=', '>'};
int v1,v2,v3,v4,v5;
printf("Enter the five integer values: ");
scanf("%d %d %d %d %d", &v1, &v2, &v3, &v4, &v5);
printf("%d ", v1);
printf("%c %d ", a[sign(v1 - v2) + 1], v2);
printf("%c %d ", a[sign(v2 - v3) + 1], v3);
printf("%c %d ", a[sign(v3 - v4) + 1], v4);
printf("%c %d \n", a[sign(v4 - v5) + 1], v5);
return 0;
}


Solved?

I suspect the question was worded incorrectly. There is no way this was the intended solution if you haven't even been introduced to relational operators and if statements yet. And if this WAS the intended solution, your professor is a raving lunatic.

Either way, please shoot your professor for me.
>>
>>53090636
>>53093459
are these possible without arrays? they dont come up until chapter 8
>>
>>53093424
Inside the function compare() you're declaring variables comp12, comp23, etc.

When the function compare() ends, those variables will be lost, because they were declared in that function. The function ended, everything local to that function is lost.

What you probably want to do is use the comp12, comp23, etc variables that you're declaring in the main() function, pass them as arguments by reference into the compare() function. Then when the compare function assigns a value to comp12, that value will be assigned to the comp12 that was declared in the main() function.
>>
>>53093516
Yes, add 61 to the result of sign()
>>
>>53093424
Results in undefined behavior if the input is zero. Division by 0 is a no-no.

And I'm not sure what your compare function is doing. What on Earth does the output signify? You need to get it to be the appropriate character and right now I have no idea what it is.

>>53093459
You might not be allowed to use arrays. So here's a modified version that doesn't use them:

#include <stdio.h>

//get the sign of a number using some bit twiddling fuckery
//returns -1 if x < 0
// 1 if x > 0
// 0 if x == 0
int sign(int x)
{
return ((unsigned)-x / 2147483648) - ((unsigned)x / 2147483648);
}

char compare(int a, int b)
{
int comp = sign(a - b);
//get the appropriate character using some fuckery
//might be implementation-dependent
char result = 61 + comp;
return result;
}

int main()
{
int v1,v2,v3,v4,v5;
printf("Enter the five integer values: ");
scanf("%d %d %d %d %d", &v1, &v2, &v3, &v4, &v5);
printf("%d ", v1);
printf("%c %d ", compare(v1, v2), v2);
printf("%c %d ", compare(v2, v3), v3);
printf("%c %d ", compare(v3, v4), v4);
printf("%c %d \n", compare(v4, v5), v5);
return 0;
}


I also added comments for the hacks.
>>
>>53093674
>return ((unsigned)-x / 2147483648) - ((unsigned)x / 2147483648);
what is the unsigned?
>>
>>53093758
it's a cast


I think.
>>
>>53093758
Casts from an integer to an unsigned integer. It's bit twiddling fuckery.

If you want to understand it, read up on two's complement. That is the number representation that is used on most modern computers.
>>
>>53093758
>>53093862
It's also going to be the reason the prof knows that you didn't write this code. hah.

Although I did learn two's complement notation in a C class, it wasn't in the first four chapters of the book.
>>
>first 4 chapters without covering logical operators
wot
>>
>>53093919
No person four chapters into an introductory C book could solve this problem.
>>
>>53093973
this, your prof is power tripping
>>
>>53093947
It's been many years, but I don't recall it being taught early.

Maybe the OP can chime in.
>>
>>53093988
I think it's more likely they described the problem wrong.

The professor COULD be a lunatic, though.
>>
>>53093862
well it works, and two's complement is in an appendix, so you could make the argument that it's not technically "outside" chapters 1-4
im gonna try to figure something else out tomorrow but if i cant i'll just say i asked my dad for advice
>>
>>53093298
>2147483648
>Implying the OP is supposed to know about that magic number

That's not gonna work.
>>
>>53094115
I'm sure it's one of those cases where it's actually very simple but we just don't see it.
Anyway, keep us posted on the solution, I'm interested.
>>
>>53093298
Is the int type always 32 bit long in C? I mean we could use INT_MAX to avoid this, but OP might not know about it.
Thread replies: 85
Thread images: 8

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.