C# program using an Array of Structs

Code with C | Programming: Projects & Source Codes Forums C# Programming C# program using an Array of Structs

This topic contains 4 replies, has 1 voice, and was last updated by  SapnaVishwas 2 years, 8 months ago.

  • Author
    Posts
  • #10390

    Miranda65M
    Member

    Write a C# program that receives the following information from a set of students:

    The application should also display the information of all the students once the data is entered.

    Implement this using an Array of Structs.

  • #10391

    Abhey
    Member

    i did the code with a friends assistance….but i haven’t completely understood it.

  • #10393

    ShikhaTan
    Member

    I’ve altered your program slightly, but I didn’t run it, so it isn’t tested. But it should work.

    In my opinion, it’s better (and easier) to use lists instead of arrays, so I build my example on lists, which will be converted to an array later on. This isn’t my best code either. I used most of your code. (And it’s been a while since I’ve coded C# Razz)

  • #10394

    Amit
    Member

    The code itself is very messy and not really easy to understand. I think you should go back and rethink your design and requirements. Write code, iterate and experiment. Breaking down problems is the most important skill you can develop as a programmer. Simple code is maintainable, fast and far more likely to be correct than complex structures.

    Let’s see what we want here;
    Enter student data
    Print registered students (which means we need to save it somehow)

    Start from the simplest program;
    First iteration
    User can enter student information.
    Application quits.

    Second iteration
    User can select to enter student information or quit application
    While user has not selected quit, let user enter student information

    Third iteration
    User can select to enter student information or quit application (Display how many students are registered)
    While user has not selected quit, let user enter student information and save to array

    Note about this iteration, I’ve not made it to print the student array because it’s too big of a requirement for a single iteration. Instead I’ve made a simpler request (to display the total of registered students) which will force us to somehow save the students into an array.

    Fourth iteration
    User can select to enter student information, print registered students’ information or quit application (Display how many students are registered)
    While user has not selected quit
    if user selected to enter information let user enter student information and save to array
    if user selected to print student information, for each student entry in the students array print information

    And well, that’s it. Each iteration builds on the previous one (you’ll need to refactor and refine code as you go though). For example, the first iteration will not need any methods other than the main one but for the second iteration we see that we want to perform the same thing over and over again so it’s better to refactor it into another method. For the third iteration we see that we need to save our data but then how do we return all that information from our method? make a container structure (struct) for the information so we can pass it around as a single logical unit…

    And one last thing, one of the most important things in your code are the names you give to everything that is used, from classes, structs and variables to methods and namespaces. Learn about camel and pascal cases or separate using underscores but make sure that everything is readable and understandable. For example, what the heck is “onef” namespace??

  • #10395

    SapnaVishwas
    Member

You must be logged in to reply to this topic.