|
How do I write a C# code to calculate the average of all scores entered between 0 and 100. Use a sentinel – controlled loop variable to terminate the loop. After values are entered and the average calculated, test the average to determine whether an A, B, C, D, or F should be recorded. The scoring rubric is as follows:
A 90-100
B 80-89
C 70-79
D 60-69
F < 60
N.B Am very new to programming language and I am interested to become C# programmer.
your kind assistance in solving this problem will be appreciated.
DammyMulero
|
|
|
|
|
DammyMulero wrote: Am very new to programming language and I am interested to become C# programmer.
In that case you should start at the beginning and study the language basics before trying more advanced projects. I would suggest .NET Book Zero[^] as a great place to begin your studies.
|
|
|
|
|
|
Go here [^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Hope he gets the picture
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
You're question looks like a homework question which in itself is not a reason for downvoting, but the lack of information is.
You have a problem (real life or homework doesn't matter) and you need an answer. What did you do to solve this problem yourself? Is the problem here that you have tried some things, but couldn't? And why not? Or don't you know how or where to start? If you want to start out programming you must have a certain idea of what you want and what direction you will take, correct? So what is that?
To answer the question, I'm a beginner I don't know where to start.
Buy a book. seriously, the internet is not good to learn new stuff from scratch. When you start out on chapter one, it will probably guide you through a "Hello World" program and gradually work up to a higher level. The problem you have here will be solved before you reach the end of the book, it's pretty basic. If you do have a problem with an exercise, come back here and ask the question, explaining what you did and ask what you can do to solve it.
Hope this helps.
V.
|
|
|
|
|
DammyMulero wrote: How do I write a C# code
By first learning the language of choice, C# in this case. Then you may begin by making notes on pieces of paper. But eventually you will need to enter the actual code in some sort of text editor (such as Notepad), perhaps even a full-blown and very complex application called an "Integrated Development Environment", but that may be beyond your abilities at this point.
|
|
|
|
|
Hi.
Please consider the following fixed values...
unsigned int value1 = 396;
unsigned int value2 = 398565;
1) The above values will change, but value1 will always be 3 digits, and value2 will always be 6 digits.
How do I....
A) Place both values into one DWORD?
b) Encode it as a 4 byte array.
So, as an example (not actual values)
396 + 398565 -> (DWORD) = 396398565 -> BYTE[] = {0x00, 0x00, 0x00, 0x00 } (whatever the hex would be)
And then, I would need to reverse it, although I think I have the code working to do that, but cant test it properly until I can get the above to work.
Thank you,
Steve
|
|
|
|
|
I still don't see why you insist on the array of bytes.
Here's a rather simple way without unsafe code:
uint a = 396 ;
uint b = 398565 ;
uint c = ( a << 20 ) | ( b & 0x0FFFFFu ) ;
byte[] d = new byte [ sizeof(uint) ] ;
for ( int i = 0 ; i < d.Length ; i++ )
{
d [ i ] = (byte ) ( ( c >> 8 * i ) & 0x0FF ) ;
}
uint e = 0 ;
for ( int i = 0 ; i < d.Length ; i++ )
{
e |= (uint) ( d [ i ] << 8 * i ) ;
}
uint f = e >> 20 ;
uint g = e & 0x0FFFFFu ;
|
|
|
|
|
FYI: I know you're not fond of converter classes, however
BitConverter.GetBytes() and
BitConverter.ToUInt32() are exactly what you want here.
|
|
|
|
|
Luc Pattyn wrote: what you want
What I want?
Actually, I also looked at some code I wrote a while back that does that sort of thing, and I found that the existing tools didn't work properly with some types (e.g. enumerations, DateTime), so I wrote my own.
Here's a taste of that technique:
private static unsafe byte[]
ToByteArray
(
object Value
,
byte[] Buffer
)
{
System.Runtime.InteropServices.GCHandle h =
System.Runtime.InteropServices.GCHandle.Alloc
( Value , System.Runtime.InteropServices.GCHandleType.Pinned ) ;
try
{
byte* b = (byte*) h.AddrOfPinnedObject().ToPointer() ;
for ( int i = 0 ; i < Buffer.Length ; i++ )
{
Buffer [ i ] = b [ i ] ;
}
}
finally
{
h.Free() ;
}
return ( Buffer ) ;
}
I may have to try this BitConverter, but I doubt I'll like it.
What I presented, though, is obviously very simple, about the minimum required for the stated task.
|
|
|
|
|
Hi Luc,
This time I was going to use BitCOnverter, but because the first problem was to get 2 int values into one uint, uint32 or whatever I would need, and then convert to a byte array.
Thank you,
Stephen
|
|
|
|
|
Ah, I see that the BitConverter only works with a few, rarely used, datatypes (and not strings! ) -- I want (not need) a more general solution.
|
|
|
|
|
Hi.
This does not seem to work.
It produces the following uint:
129098304
and I need 396398565
Thank you,
Stephen
|
|
|
|
|
|
BitConverter.GetBytes(a + 1000000*b)
|
|
|
|
|
|
Hi,
Sorry, this produces a different result than required.
uint val1 = 583;
uint val2 = 728439;
I should have 583728439, which sould be (in bytes) 0x22CAFD37.
Am I doing something wrong?
Thank you,
Stephen
|
|
|
|
|
|
You can combine them using division and modulo operations:
const uint shift = 1000000;
uint val1 = 539;
uint val2 = 666666;
uint combined = (val1 * shift) + val2;
uint restore_val1 = combined / shift;
uint restore_val2 = combined % shift;
As for getting the byte array, you can use the BitConverter:
byte[] combined_bin = BitConverter.GetBytes(combined);
|
|
|
|
|
Thank you very much.
That works perfect.
Steve
|
|
|
|
|
Hi.
The following function was taken from c++ code, and I have tried to adapt it for my c# code, however, I am not sure if it is doing the same thing?
The full c++ function is...
void ShowProdKey(unsigned char *ProdKey)
{
unsigned int Val1, Val2;
Val1 = *(unsigned int *)ProdKey;
Val1 &= 0x7fffffff;
Val1 >>= 1;
Val2 = CheckDigit(Val1 % 1000000);
Val1 /= 1000000;
printf("Your product ID should be XXXXX-%.3u-%.7u-XXXXX.\n", Val1, Val2);
}
and my adapted function is the following (it works as in as far as it is producing a result which looks correct, but I am unsure if the changed part of the function is doing the same as in the above code)
void ShowProdKey(byte[] prodKey)
{
uint val1, val2;
val1 = BitConverter.ToUInt32(prodKey, 0);
val1 &= 0x7fffffff;
val1 >>= 1;
val2 = CheckDigit(val1 % 1000000);
val1 /= 1000000;
string output = ("");
output = val1.ToString();
output += val2.ToString();
MessageBox.Show(output);
}
So the part I changed was this in c++
Val1 = *(unsigned int *)ProdKey;
To this...
val1 = BitConverter.ToUInt32(prodKey, 0);
The output should be a double word formatted something like...
000 - 0000000
My version does produce a value like this, so I think it may be doing the same thing?
Also, how would I write code to reverse the function?
Or, in other words, the above function takes 4 bytes and produces a DWORD, which it formats into two unsigned ints...
How would I write a function that would take two unsigned ints, and produce a DWORD, and then encode that into 4 bytes, the reverse of the above function?
Thank you VERY much for your help...
Stephen
|
|
|
|
|
stephen.darling wrote: Val1 = *(unsigned int *)ProdKey;
Can probably be done with unsafe code.
stephen.darling wrote: Val1 >>= 1;
Probably makes it non-reversible (except with a 50/50 probability of being correct).
stephen.darling wrote: CheckDigit
What does that do?
What is it trying to do? It doesn't seem to use all the bits that are sent in.
|
|
|
|
|
Hi,
Well, using that function, if the 4 bytes are:
0x22CAFD37, the function should produce the following number:
583728439
Now, using the c++ version it works correct, but my c# version does not produce the correct value, it does produce a 9 digit number, but not that one.
However, if I try to use:
Val1 = *(unsigned int *)ProdKey;
in my c# version, it wont compile, so not sure if the code I changed is doing something else?
To answer what it is, let me explain...
My keygenerator generates the above 9 digit number, as a kind of product ID value, and encodes it into a 4 byte array to build part of a serial number..
When the serial number is entered into my application, the 4 bytes are decoded back into a dword, and the above function formats it into the 9 digits shown, and my app checks them,
It is working, and 9 digits are produced, but they are incorrect????
Also, how do I reverse the code to get two int values into one DWORD, and then encode it back into a 4 byte array?
Thank you,
Stephen
|
|
|
|
|
The BitConverter code looks correct. (Just like a cast in C, though, it will give a different answer if the machine has the opposite endianness.) Your code is doing more than just that – what is CheckDigit doing?
|
|
|
|
|