Jump to content

Questions about some codes

Hagge

Hello

 

I'm doing some questions I've got on a paper a while back, when I was going to school..But never did them:) And now I've decided to learn some. I found those papers at school when I was going there, so there are not questions I was supposed to learn..I've on the other hand decided to learn them :) And I've come upon 2 problems, which I cant find any good answers for.:(

 

I would be greatfull for som nice and explaining answers :)

 

First of all, whats wrong with this code, why those are wrong and how to fix them

http://pastebin.com/n79gYq6N

 

Second

This is a code I cant compile, why?

http://pastebin.com/5Uyx0J2m

 

 

Those are the question I got problems with...I've answered all the other questions, which is about 20 of them:)

Help would be awesome and I've would be greatfull!

 

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't used that language before, but judging by my experience with C# don't you need a semicolon after you declare your variables?

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't used that language before, but judging by my experience with C# don't you need a semicolon after you declare your variables?

Not sure, I've got the code in Microsoft Visual 2010, and I'm seeing some red lines..

"Slinga" is one of them...According to MV it does not exist in the current context..

"else" and "tal1" is also wrong. "Addera" dosent exist too..

The middle Writeline before "(addera....) is also wrong..

Link to comment
Share on other sites

Link to post
Share on other sites

In C# you also need { } brackets to contain the actions that you would like the program to perform if the if statement is true. For instance,

 

if (tall == 56)

{

     Console.Writeline(addera(slinga, tall));

}

else

{

     tall = 56;

}

Link to comment
Share on other sites

Link to post
Share on other sites

Are you talking about the first or the second one:)?

I'm referring to this line.

 

int tal1, tal2;

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure, I've got the code in Microsoft Visual 2010, and I'm seeing some red lines..

"Slinga" is one of them...According to MV it does not exist in the current context..

"else" and "tal1" is also wrong. "Addera" dosent exist too..

The middle Writeline before "(addera....) is also wrong..

You haven't declared Slinga as anything. What exactly is the program meant to do?

 

The "else" error should disappear once you implement the changes with the { } brackets, as stated above.

 

I have no idea what "tal1" and "Addera" are meant to be doing.

Link to comment
Share on other sites

Link to post
Share on other sites

Edit: As for the second program that can't be compiled, a "double" cannot be stored in to a variable with less data stored, in this case "int", resulting in an error.

Link to comment
Share on other sites

Link to post
Share on other sites

As for the second program that can't be compiled, an error results because the variable "b" has not been declared. Every variable must be declared before it can be used.

I did as u said, still got errors..Even my Console.Writeline is an error..

Namnl_s.jpg I dont know what it's ment for, in the paper it just says the code, and asks whats wrong with it..? Explain:/

 

The second one is that simple?

Link to comment
Share on other sites

Link to post
Share on other sites

Haven't used this language before, but its syntax looks fairly C-like, as Dave

has mentioned, so here's what I'd change/clarify going by that:

 

using System;    class program    {        static void Main(string[])        {            int tal1, tal2; // Add semicolon                tal2=convert.ToInt32(Console.Readline());                // I'm assuming it's supposed to spell convert.            for (int slinga = 0; slinga < 2; slinga++)            // add type declaration for slinga            {                if (tal1 == 56)                // Where did tal1 get its value?                // If tal1 has no value at this line, there will be                // an error, of course.                // EDIT: Tried this quickly, this might depend on the language.                // In C this would actually work.                { // add braces around this block of code.                    Console.Writeline(Addera(slinga, tal1));                    // Addera is capitalized below, so I've changed                    // addera to Addera here.                    tal2--;                }                else tal1 = 56;            }        }        static int Addera(int tal1, int tal2)        {            return tal1 + tal2;        }    }
It's of course a bit tricky since I don't know this code's context, but that's what

I would change knowing what I know at the moment.

As for the second bit of code, what's the context of that piece of code (meaning the

bits and bobs that go around it)? The code itself doesn't look wrong, and depending

on language can be compiled without error (C, for example, and I have tried this, just

to be sure ;)).

 

double a = 56.5;int b=a;
EDIT: It might be that the language you're working in is finicky about

implicit type conversions from double to int.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

I did as u said, still got errors..Even my Console.Writeline is an error..

Namnl_s.jpg I dont know what it's ment for, in the paper it just says the code, and asks whats wrong with it..? Explain:/

 

The second one is that simple?

I only told you what I knew was wrong with my limited knowledge in programming (I'm in Grade 11 University Programming at the moment). I'm not entirely sure what the proper syntax for the other parts are, and yes, the second program should compile now.

Link to comment
Share on other sites

Link to post
Share on other sites

Haven't used this language before, but its syntax looks fairly C-like, as Dave

has mentioned, so here's what I'd change/clarify going by that:

using System;    class program    {        static void Main(string[])        {            int tal1, tal2; // Add semicolon                tal2=convert.ToInt32(Console.Readline());                // I'm assuming it's supposed to spell convert.            for (int slinga = 0; slinga < 2; slinga++)            // add type declaration for slinga            {                if (tal1 == 56)                // Where did tal1 get its value?                // If tal1 has no value at this line, there will be                // an error, of course.                { // add braces around this block of code.                    Console.Writeline(Addera(slinga, tal1));                    // Addera is capitalized below, so I've changed                    // addera to Addera here.                    tal2--;                }                else tal1 = 56;            }        }        static int Addera(int tal1, int tal2)        {            return tal1 + tal2;        }    }
It's of course a bit tricky since I don't know this code's context, but that's what

I would change knowing what I know at the moment.

As for the second bit of code, what's the context of that piece of code (meaning the

bits and bobs that go around it)? The code itself doesn't look wrong, and depending

on language can be compiled without error (C, for example, and I have tried this, just

to be sure ;)).

double a = 56.5;int b=a;
EDIT: It might be that the language you're working in is finicky about

implicit type conversions from double to int.

In C# the second program would result in an error, I'm pretty sure that his teacher meant for that program to demonstrate the students' fundamental understanding of programming.
Link to comment
Share on other sites

Link to post
Share on other sites

in C# the second program would result in an error.

Which one, top, bottom or both?

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Interesting. I'm not familiar with C#, so any suggestions I've made are based on

C/C++ experience and general code intuition (or whatever one chooses to call it ;)).

Is this C#, by the way?

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

I will try all out, and come back and Yes its C# :)

Link to comment
Share on other sites

Link to post
Share on other sites

I will try all out, and come back and Yes its C# :)

As for the second program that can't be compiled, a "double" cannot be stored in to a variable with less data stored, in this case "int", resulting in an error. (I reposted it in case you missed it)

Link to comment
Share on other sites

Link to post
Share on other sites

I will try all out, and come back and Yes its C# :)

Good to know. On another note, it might be possible that you also need

braces around that single line in the else statement, as Dave has written

in his post, not sure how C# handles that (you wouldn't need it in C).

Another thing regarding the second program: Storing a double in an int will

either lead to an error if it's not allowed or loss of information. In C, you

can do this:

double a=10.231;int b=a;printf("%d\n",b); // Will print 10.
The decimal places are just lost, but the operation itself is allowed. I

don't know how C# handles this though.

EDIT:

Ninja'd :ph34r:

See Dave's post above :)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

 

Another thing regarding the second program: Storing a double in an int will

either lead to an error if it's not allowed or loss of information.

C# cannot handle this, as I stated above. (silly, isn't it?)

Link to comment
Share on other sites

Link to post
Share on other sites

Okey, I got it! :) Thanks Alot!:) so far so good!:)

Link to comment
Share on other sites

Link to post
Share on other sites

Okey, I got it! :) Thanks Alot! :) so far so good! :)

Nice job, do have any other questions?

Link to comment
Share on other sites

Link to post
Share on other sites

C# cannot handle this, as I stated above. (silly, isn't it?)

I suppose it's a bit of a reflection of Microsoft's philosophy to protect the user

(or the coder in this case) from themselves. Implicit type conversions can be quite

an excellent source for bugs. But they can also be rather practical in some cases.

Personally I'm more the kind of person who likes to have the most power possible

which allows me to make more mistakes. Makes for a bit of a steeper learning

curve, but it usually yields better results when a skilled person (I'm not saying

that I am, but I hope to be at some point in some fields ;)) is doing proper work.

So yes, I agree that it's a bit silly, even though I can imagine at least some

reasons for making things this way.

EDIT:

Okey, I got it! :) Thanks Alot! :) so far so good! :)

Holy pink rhino, this thread is fast :o :lol:

Anyway, good to know, happy to help with more if needed. :)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Nice job, do have any other questions?

 

 

I suppose it's a bit of a reflection of Microsoft's philosophy to protect the user

(or the coder in this case) from themselves. Implicit type conversions can be quite

an excellent source for bugs. But they can also be rather practical in some cases.

Personally I'm more the kind of person who likes to have the most power possible

which allows me to make more mistakes. Makes for a bit of a steeper learning

curve, but it usually yields better results when a skilled person (I'm not saying

that I am, but I hope to be at some point in some fields ;)) is doing proper work.

So yes, I agree that it's a bit silly, even though I can imagine at least some

reasons for making things this way.

EDIT:

Holy pink rhino, this thread is fast :o :lol:

Anyway, good to know, happy to help with more if needed. :)

Well there is something :)

Im creating a game, which I made a topic for a couple of days ago, but the topic died, and I didnt paste any of the codes there..I I pasted some codes, but not as much as im about to now. I got some help from a guy here, and he did help some, but not the whole thing :( I can maybe get some help from you guys :) and I can maybe be done with it faster than I thought

 

I'm making a game, Hangman, and now I've translated the words to get ur help

The goal of the game is to make 4 options,

 

- Put ur own words

- List all the words

- Play

- Quit

 

And when you press 3 to play, it will ask you how many chances you want before losing. Say 2 for example :)

And When the game starts I want it to say 0/2 or something...

 

The problem is now, It dosent ask how many chances you want..

When I put my own words, and press enter, in option 1, nothing happens...

And when I play, the 0/2 and then 1/2 dosent show up...

When I start the game it starts by saying guess..

When you guess wrong, I want it to say, Try again..Not guess..

 

I got 3 files so far. And i've made this game by myself...I've got some help my "Majorawesome". He helped me with chances..He did a 5th option where u choose how many chances u want..But Thats not my goal...My goal is to make the game ask you how many chances u want, after pressing "Play"...

 

I hope you can help me with this one ;)

 

Here are the codes..

 

The Menu - http://pastebin.com/cTasZDM9

WordList - http://pastebin.com/6w8kY2LX

The System - http://pastebin.com/RWy1jhmu

Link to comment
Share on other sites

Link to post
Share on other sites

I'll take a look at it, sure. Since I don't really program in C#, I probably can't help

you that much with the programming itself, but I should be able to spot algorithmic and

syntax errors and maybe give a few tips here and there. ;)

I'm very busy at the moment putting together my new PC, so it might take me a day or two.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Well there is something :)

Im creating a game, which I made a topic for a couple of days ago, but the topic died, and I didnt paste any of the codes there..I I pasted some codes, but not as much as im about to now. I got some help from a guy here, and he did help some, but not the whole thing :( I can maybe get some help from you guys :) and I can maybe be done with it faster than I thought

 

I'm making a game, Hangman, and now I've translated the words to get ur help

The goal of the game is to make 4 options,

 

- Put ur own words

- List all the words

- Play

- Quit

 

And when you press 3 to play, it will ask you how many chances you want before losing. Say 2 for example :)

And When the game starts I want it to say 0/2 or something...

 

The problem is now, It dosent ask how many chances you want..

When I put my own words, and press enter, in option 1, nothing happens...

And when I play, the 0/2 and then 1/2 dosent show up...

When I start the game it starts by saying guess..

When you guess wrong, I want it to say, Try again..Not guess..

 

I got 3 files so far. And i've made this game by myself...I've got some help my "Majorawesome". He helped me with chances..He did a 5th option where u choose how many chances u want..But Thats not my goal...My goal is to make the game ask you how many chances u want, after pressing "Play"...

 

I hope you can help me with this one ;)

 

Here are the codes..

 

The Menu - http://pastebin.com/cTasZDM9

WordList - http://pastebin.com/6w8kY2LX

The System - http://pastebin.com/RWy1jhmu

I think the solution would be to replace this line with an else statement.

 

  1. if (!guessRight) //Wrong Control
  2.                 {
  3.                     guessWrong++;
  4.                 }

 

Basically what you're trying to do is get the program to

 

1. know that the answer was wrong

2. show a "Try Again" message

 

if I understand correctly. In order to do that, you could replace this line and saying this.

 

else

{

     guessWrong++;

}

 

I'm not familiar with a portion of the code, so I'm not sure whether you would tell the program to show a "Try Again" message here as well, or if that is coded somewhere else in the program.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×