Question about Arrays..

Discussion in 'Software' started by collegestudent, Nov 5, 2009.

  1. collegestudent

    collegestudent Private E-2

    Ok, this is all puesdocode and the chapter is about working with Arrays and multi dimentional arrays, I have an assignment due soon and am not sure if the code I have for one of the questions is correct.. here is the code and question.. (Is there anyway you can see to make the code better or shorter?)

    Code:
    Question: Total sales -
    Design a program that asks the user to enter a store's sales for each day of the week. The amounts should be stored in an array. Use a loop to calculate
     the total sales for the week and display the result. Here is the puesdocode I have developed so far.. and am not sure if it is right, since it is not an actual language it is hard to find out the answer..
    
    // the main module
    Module Main()
    	//declare constant variable
    	Constant Integer SIZE = 6
    	//declare variables
    	Declare String day[SIZE] = "Sunday",  "Monday", "Tuesday", "Wednesday", 	"Thursday", "Friday", "Saturday"
    	Declare Real totalSales
    	Declare Real result
    	Declare Integer index
    	// body of the module
    	For index = 0 to SIZE - 1
    		Display "Please enter the total sales amount for ", day[index], ":", day[SIZE] + 1, "
    		Input totalSales[index]
    	End For
    	Call theResults
    	Display "The total sales for the week is $", results, ."
    End Module
    // function to return the total amount of money made for the week
    Function theResults(day, totalSales, results, index)
    	Set results = totalSales[index] * day[SIZE]
    	Return results
    End Function
    
    I know there has to be an error or two in there somewhere, can any help? thanks!
     
  2. SWario

    SWario Sergeant

    Why pseudocode? What language is the class/assignment in? Why are you defining SIZE (Constant Integer SIZE = 6)? Are you defining an Array as a String (Declare String day[SIZE]), if so, why? When is "soon"? Is this program supposed to be run with a GUI or in a command line?

    I'll take a look at it.
     
  3. collegestudent

    collegestudent Private E-2

    It is a pre-resquite class before I can get into the good stuff like Visual basic, .NET, Java ect for my 2 yr degree for computer science / programming.

    It is more about learning programming theory, and what functions, loops, arrays, classes. defensive programming, ect.. is.. this chapter I am having a problem with is the array chapter, and multi dementional arrays.

    I am defining SIZE as a constant integer value, since arrays start from 0 I put the size as 6. 0=Sunday,1=Monday,ect.. till 6 = Sat.

    I am defining the array with a constant varialble which is an integer, and then the variable day with an array of the constant variable of 6 to avoid a off by one error.

    I'm thinking the main part is I am not sure if the FOR statement is correct, and if you or anyone has better advise on how to make this program with cleaner and less code. I appriciate any help, I am not very good at math, next semester I am taking a Algerbra class. thank you
     
  4. SWario

    SWario Sergeant

    What, no C?

    I think the only way to effectively learn programming theory is to actually program. Alas, I can't change the structure of your class.

    Why not just use [6] instead of using a constant? You're not going to reuse it, so why define a constant that's going to take up unnecessary space? Keep in mind that not all programming languages require you to define the size of an array (or other variables) at initialization.



    You didn't follow the instructions for the assignment.
    This you followed. Though you shouldn't store daily sales amounts in an array called "totalSales". The array isn't storing "totalSales" amounts, so call it something more descriptive, like "sales" or "dailySales".

    This you didn't follow; you used a function instead of a loop, and the function won't return a total but just attempt to return "(Saturday's sales amount)" times "Saturday". Why are you using a function anyway? You aren't repeating the process of totaling for other weeks, so there's no need for a function.

    Below is some pseudocode that I threw together:
    Code:
    Main
    	Array days = ("Sunday", "Monday", "Tuesday", "Wednesday",
    	 "Thursday", "Friday", "Saturday");
    	Array sales;
    	Real totalSales;
    	Integer index;
    	For index = 0 to (sizeof(days) - 1)
    		Print "Please enter the sales amount for ", day[index], ": ";
    		Input sales[index];
    		totalSales = totalSales + sales[index];
    	End For
    	
    	Print "The total sales for the week is $", totalSales, ".";
    End
    
     
  5. JJJIrish05

    JJJIrish05 Sergeant

    also 'sales' doesn't need to be an array since there is no need to remember how much money was made on each day, so it could just be a real

    also collegestudent, when you set the size of the array you always want to put in the total size of the array so "array days[7]" will go from days[0] to days[6]. "array days[6]" will be an array from days[0] to days[5].
     
  6. SWario

    SWario Sergeant

    Actually, the instructions he provided explicitly stated that he should be storing the sales amounts for each day in an array. Otherwise, I would agree with you.
    And yeah, I missed the array size thing. I should have paid more attention to the value of the constant he was using instead of the fact that he was using a constant.
     
  7. collegestudent

    collegestudent Private E-2

    I wish.. the teacher told me that if the school had a C programming class there would only be like 4 students in it. I agree, last semester the same course used Alice and Visual basic to teach, I am one of the "lucky" students that gets to use this new crappy book :)


    Why not just use [6] instead of using a constant? You're not going to reuse it, so why define a constant that's going to take up unnecessary space? Keep in mind that not all programming languages require you to define the size of an array (or other variables) at initialization.


    I was looking at an example in the book and it used a constant, so that is why I used it. I see what you are saying though, it is just more code then their needs to be. I changed my code around now..

    I see what you are saying about the names of the variables, it was kind of confusing what I named them, so I took your advice when recoding it.

    If the FOR statement considered a loop? What do you think about the new version?

    Code:
    // the main module
    Module Main()
    	//declare variables
    	Declare String day[6] = "Sunday",  "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    	Declare Real sales
    	Declare Real totalSales
    	Declare Integer index
    	// body of the module
            Set index = 0
    	While index = 0 and days[6] - 1
    		Display "Please enter the total sales amount for ", day[index], ": "
    		Input sales[index]
    		Do until index = 6 and days[0]
    	End While
    	Set totalSales = totalSales + sales[index]
    	Display "The total sales for the week is $", results, ."
    End Module
    
    JJJIrish05, what do you mean by that? Does the array always count from 0 to whatever number it is declared too? so it is always 0,1,2,3,4,5,6 instead of 6,5,4,3,2,1 or 1,3,2,4,6,0,5?..
     
    Last edited: Nov 6, 2009
  8. SWario

    SWario Sergeant

    That's because no one wants to learn C. They are looking at the wrong languages. They should be teaching something more modern if they want to generate interest in programming in general. The best choices for these are usually Java, a .NET language, or Adobe Flex. On second thought, wait, this is for a Computer Science degree, and the students don't want to learn C? What the heck?

    Something to keep in mind: book examples aren't always the most effective means of accomplishing a goal, but usually seek to demonstrate a lot of different topics.

    Always name variables (and methods/functions, and classes) something that is descriptive about what it is meant to do.

    FOR is a kind of loop. After all, the code inside a FOR statements repeats, or "loops", doesn't it? FOR, WHILE, and DO-WHILE are the most common loops. There are some other variants of FOR, and I'm sure there are other types of loops I've missed.

    You're still initializing the days array one element short. Days should hold seven (7) items, one for each day of the week. So it should be:
    Code:
    days[7]
    to indicate that it has a size of seven elements. You also don't generally need to typecast an array as something else (String, Int, etc.), as arrays are usually their own variable type (Array). Is this something the book is showing?
    Code:
    While index = 0
    This check will be false as soon as the Index gets incremented to 1. Ironically, the code in the loop never increments the Index, so you will repeatedly be entering in the sales data for Sunday.
    Code:
    days[6] - 1
    The above is going to return something that is not an Integer, and it will likely break the WHILE loop.
    Code:
    and days[0]
    What is the above meant to accomplish?
    Code:
    Set totalSales = totalSales + sales[index]
    Once again, because you have taken this outside of the loop, it will only evaluate to:
    Code:
    Set totalSales = (null) + sales[6]
    [COLOR="Green"]// the last known value of "index" is 6 by the end of the WHILE loop[/COLOR]
    It will not loop through all of the values input for sales, and you've once again ignored the instruction:
    Code:
    Display "The total sales for the week is $", results, [COLOR="Red"]."
    What the heck is that stray period there for?

    Arrays are almost always zero-based, meaning their first element is at Index 0 (days[0] is "Sunday"). If you define days as days[6], you are saying you only want it to be able to store 6 items, meaning the highest Index value it will accept is 5 (6-1, because array elements start at 0, not 1; so instead of 1,2,3,4,5,6 - you have 0,1,2,3,4,5). This leaves you unable to store all seven days of the week. You want to initialize days as days[7], telling the program to allow the array to store SEVEN elements.
     
  9. collegestudent

    collegestudent Private E-2

    I agree with you, I would love to learn C, I got that book Sams Learn C++ in 21 days but it was confusing for me. I am sure when I am working on my 4 year degree for computer science after I finish my 2 year the University will offer a course on C and C++.

    Yes, It is hard for me to learn by just reading a book, I am more of a visual, kenetic learner.

    Ok, so that is probuly what the teacher wants for a "Loop" is for me to use the FOR, WHILE or DO-WHILE statement to complete the assignment. I was not very informed on the FOR statement and re-read that information.

     
  10. SWario

    SWario Sergeant

    When you initialize and Array, the number you place in the square brackets ([#]) defines the SIZE of the array (the number of elements it can hold)If you initialize the Array days as days[6], it can only hold 6 elements, and it will have 6 different Indexes available, starting from zero: 0, 1, 2, 3, 4, 5. If you initialize the Array days as days[7], it can hold 7 elements, and it will have 7 different Indexes available, starting from zero: 0, 1, 2, 3, 4, 5, 6. Does that make more sense?


    It seems weird to me, because you usually declare Arrays as Arrays, just as you would declare Strings as Strings or Integers as Integers. Does the book show any examples of declaring Arrays? How does it show to do it?


    If you are going to iterate a known number of times, you should use a FOR loop. If you are going to iterate until a certain condition changes, you should use a WHILE loop. In this case, I recommend a FOR loop, since you know you only have to iterate seven times (one for each day of the week). You are more likely to accidentally end up in an infinite loop using WHILE than you are with FOR.

    You can write a function, and run the function in a loop, or you can write the loop in the function. In this case, because the point of the function would be to total the week's daily sales, you would put the loop in the function, and simply call the function once in the program. A function is not necessary for this assignment. Depending on your teacher, they may dock you points for complicating things (it's not in the instructions I handed out!), or they may be happy that you wrote a function (the student is thinking ahead!). I don't know your teacher, so I provided the safer way in my original instructions. If you feel they'd respond well to a function, go for it.

    The point I was making was that the condition does nothing for the WHILE loop, as it evaluates to a string. Think about what days[6] means. How would adding or subtracting one to it help define a loop? If you had put this code into a compiler, it would have given you an error, probably about variable types. Again, I really think your instructor should be letting the students get hands-on experience with code instead of theory.


    Give us a new draft of code. When is this assignment due?
    You're welcome! Let us know if there's something I've said that you don't understand.
     
  11. collegestudent

    collegestudent Private E-2

    Not really, unless 0 = NULL, always.. and no element can be placed there in the 0 slot. Because the way I see it, Days[7] would equal = 0,1,2,3,4,5,6,7
    So since 0 is being counted it always has to be minus one on the array or you are going to get a one off error.


    To declare an array, an example would be this (basics of the chapter)
    Code:
    // Declare an Integer array with 10 elements.
    Declare Integer series[10]
    
    //declare a variable to use in this loop
    Declare Integer index
    
    // set each array element to 100
    For index = 0 to 9
         Set series[index] = 100
    End For
    

    VERY helpful info, thank you :) now that makes sense. Your explanation of the difference between FOR and WHILE statements is very helpful.

    I have no idea.. this is the second course I have taken at this school, I also have completed 1 1/2 of network admin in college but decided to change to progamming and computer science, which I find alot more interesting. This is an online course so I have never met the teacher and he he generally does not help very much and gives simple one line answers to every question. He also takes forever to grade our papers so I can not compare last weeks code & work to this weeks assignment, even if I wait until the last day to turn it in, I still do not have a grade from the last assignment. Students get to grade the teacher soon, so I will say honestly whatever is going on to improve the courses.


    It is due tommarow by midnight, I am working on a new code, but also have part time job and another class I have to deal with. This class is the only one giving me trouble. I will post my final puesdocode on here tommarow, I really have to concentrate and study more..

    Thanks, you explained everything great, and once again I appriciate your guidance and help with this situation, and also for being thorough about it.
     
  12. SWario

    SWario Sergeant

    The example in your book actually does a good job of illustrating this explanation:
    Code:
    // Declare an Integer array with 10 elements.
    Declare Integer [COLOR="Red"]series[[COLOR="Blue"]10[/COLOR]][/COLOR]
    
    //declare a variable to use in this loop
    Declare Integer index
    
    // set each array element to 100
    [COLOR="Red"]For index = 0 to 9[/COLOR]
         Set series[index] = 100
    End For
    Note: The array was declared "series[10]", but the FOR loop only runs through Indexes 0 to 9. The FOR loop essentially does this:
    Code:
    Set series[0] = 100
    Set series[1] = 100
    Set series[2] = 100
    Set series[3] = 100
    Set series[4] = 100
    Set series[5] = 100
    Set series[6] = 100
    Set series[7] = 100
    Set series[8] = 100
    Set series[9] = 100
    If you count the numbers 0 to 9, you will realize that there are 10 numbers in that range. So what happens if you try to access Index 10? You will get an error, because Index 10 represents the ELEVENTH element of an array, and the array was only initialized to hold TEN elements.

    Does that make more sense?
     
  13. collegestudent

    collegestudent Private E-2

    It makes more sense, except why would days[7]?..

    and why not days[6] Like I originally posted? 0 counts as a value correct? So it would be 0 to 6 = 7 values.. not 0 to 7 = 8 values, unless 0 is NULL and does not hold an element. the subscript 0 is always NULL? it does not hold an element ever?

    Thanks for your help man.
     
  14. SWario

    SWario Sergeant

    You call days[0] when you are trying to access the first element (at Index 0) of the Array days. However, when you DEFINE or DECLARE an Array, you must do so with the NUMBER OF ELEMENTS it is meant to hold, meaning the COUNT OF INDEXES you can access. Declaring:
    Code:
    Declare String days[[COLOR="Blue"]1[/COLOR]]
    Means that the Array days will be able to store ONE element, which is accessible at Index 0 (Set/Get days[0]).

    Declaring:
    Code:
    Declare String days[[COLOR="Blue"]2[/COLOR]]
    Means that the Array days will be able to store TWO elements, which are accessible at Indexes 0 and 1 (days[0], days[1]).

    Declaring:
    Code:
    Declare String days[[COLOR="Blue"]3[/COLOR]]
    Means that the Array days will be able to store THREE elements, which are accessible at Indexes 0, 1, and 2 (days[0], days[1], days[2]).

    And so on and so forth, such that declaring:
    Code:
    Declare String days[[COLOR="Blue"]7[/COLOR]]
    Means that the Array days will be able to store SEVEN elements, which are accessible at Indexes 0, 1, 2, 3, 4, 5, and 6 (days[0], days[1], days[2], days[3], days[4], days[5], days[6]).


    If your book has a section discussing "one off errors", you may want to reread it, as I'm sure it addresses this very issue. The first element is at Index 0, the second element is at Index 1, the third element is at Index 2, and so on and so forth. An Array with 5 elements will have Indexes 0, 1, 2, 3, and 4.

    Does that make sense? If you are asking about something other than the difference between Declaring Arrays and Accessing them, like why I made a certain correction, you should probably wait until you've posted new pseudocode where I can more easily point out the error and understand where you are having a problem.
     
  15. collegestudent

    collegestudent Private E-2

    So pretty much in the array 0 does not hold any value?

    I have to turn in my code tommarow so I am still working on it..

    I took my quiz today, and seemed to have problems with these questions (took the test twice and got the same score, some different questions each time) also, when I used your advice of not useing 0 for a value I got the question wrong, so the subscript 0 should hold an element like string, integer, real, ect..

    (From what I am being told, this teacher is always late on grading work, so I can not look at what mistakes I made from the last chapter, and he just now started giving us back examples of what our code should have looked like, and what we did wrong. I can see he is trying different methods and things to teach but I am the unlucky 1st class using this new course)

    anyways.. on to the test.. i scored a 76% both attempts, so I am not getting something, here is the questions I got wrong I put a # by the answer I chose that was wrong, one has 2 #'s because I failed that same question twice. On the multiple choice questions on the second attempt the test does not tell you what the correct answers where suppose to be.

    Code:
    PICKED ANSWER == #
    
    8.
    Which of the following arguments must be passed when passing an 
    array as an argument?
    A) The array itself
    B) An integer that specifies the number of elements in the 
    array
    C) The data type of the array
    D) A and B
    E) A and C#
    	
    
    9.
    The expression score[5] is pronounced ________.
    A) Score sub 4
    B) Score of 5#
    C) Score sub 5
    D) Score 5#
    E) Score of 4
    
    
    16.
    True/False: If array name contains a list of names, name[1] is 
    the name of the first person.
    A) True#
    B) False
    
    
    19.
    What is the subscript for the data value 92 in the example 
    given below?
    Declare Integer score [5] = 83, 92, 78, 94, 71
    A) B
    B) Two#
    C) One
    D) A
    E) None of the above
    
    
    22.
    True/False: One of the advantages of two- or more dimensional 
    arrays is that the data values can be of two or more data 
    types.
    A) True#
    B) False
    
    25.
    True/False: Unlike variables, arrays need to be initialized 
    
    separately from the declaration.
    A) True#
    B) False
    
     
  16. SWario

    SWario Sergeant

    Index 0 holds the first element of the array.


    I have not given you advice that said that Index 0 will be null or empty, rather the opposite. Index 0 holds the first element of the array.

    You had this partially right before. Index 0 is the first element of the Array. What you had wrong is the size of the Array. How many days are there in a week? How many elements does the Array need to hold?

    Maybe this will help illustrate:
    Code:
    19.
    What is the subscript for the data value 92 in the example 
    given below?
    Declare Integer score [5] = 83, 92, 78, 94, 71
    ////////////////   Indexes: 0   1   2   3   4
    A) B
    B) Two#
    [COLOR="Green"]C) One[/COLOR]
    D) A
    E) None of the above
     
  17. collegestudent

    collegestudent Private E-2

    So I was correct to begin with saying that days[6]? and not days[7]?
    considering 0=Sunday, first day of the week?

    Why is it called score SUB 5? what does SUB mean? once again thanks for your help :)
     
  18. SWario

    SWario Sergeant

    If you are referring to how to initialize the Array, no, you are not correct. You are correct that days[0] = Sunday.
    The questions I posed above are meant to make you think about this.

    Array Declarations and Array Access are different. When you initialize an Array, you Declare it by placing the NUMBER OF ELEMENTS you want it to hold where you would normally place an Index (in the square brackets). When you want to read or write data in an Array, you specify the Index in the square brackets. Because the highest Index and the Array Size differ by ONE, some people get a one-off error by trying to access an Array Index that does not exist.


    I do not know why, as this is not the way I would pronounce it. I would say "Array days, Index 5" or shorten it to "days, 5". However, your book wants you to say "sub", so that is what you have to do for your coursework. "Sub" is short for "subscript", as illustrated by Question 19.
     
  19. JJJIrish05

    JJJIrish05 Sergeant

    Declare Array days[7];
    days[0]=Sunday;
    days[1]=Monday;
    days[2]=Tuesday;
    days[3]=Wednesday;
    days[4]=Thursday;
    days[5]=Friday;
    days[6]=Saturday;


    there is no days[7] but as you can see the array does have 7 elements, do you understand?
     
  20. collegestudent

    collegestudent Private E-2

    Yes, JJJIrish thank you! That is what I needed to fully understand. Simple & easy to understand.

    So there is not a days[7]=whateverDay.. it is just declared as days[7] because their are 7 elements, which count from 0-6 to = 7

    thank you also SWario for helping me with this rough chapter about arrays, and being thorough with your answers :) I beleive this case is now closed if you want, I will post my code today later, it is due by midnight tonight, then 2 weeks later I will probuly get the grade results lol
     
  21. JJJIrish05

    JJJIrish05 Sergeant

    glad you understand it now! Let us know if you ever need more help
     
  22. collegestudent

    collegestudent Private E-2

    Code:
    // the main module
    Module Main()
    Constant Integer SIZE = 7
    	//declare variables and initialize the array
    	Declare String day[SIZE] = "Sunday",  "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    Declare string day [0] ="Sunday"
    Declare string day[1] ="Monday"
    Declare string day[2]="Tuesday"
    Declare string day[3]="Wednesday"
    Declare string day[4]="Thursday"
    Declare string day[5]="Friday"
    Declare string day[6]="Saturday"
    	Declare Real sales
    	Declare Real totalSales
    	Declare Integer index
    	Set days[SIZE] = 0
    	Set index = 0
    	// body of the module and useing FOR as a LOOP to input each day until it reachs the //last day of the week then ends the FOR statement
    		For index = 0 + 1 to days[SIZE] + 1
    		Display "Please enter the sales amount for ", day[index], " : "
    		Input sales[index] until days[SIZE] == 7
    		Set totalSales = totalSales + sales[index]
    		End For
    	Display "The total sales for the week is $", totalSales, "
    End Module
    
    I have to midnight to turn this code in, the is a semi-final edition unless I or you sees stuff wrong with it later and fix it..

    that is what I have so far, maybe in a couple weeks I can post the grade or "Correct puesdocode"
     
  23. JJJIrish05

    JJJIrish05 Sergeant

    you're now setting each value of day twice, while it really makes no difference it isn't needed and will probably result in lost points so only do it one way, which ever you like best.

    This is currently not declared as an array, but you call it like an array in your loop, so change this
    This for loop is wrong, please describe to us in plain english what you think this statement means, and we'll tell you why you're wrong.
    This is also wrong, the 'FOR' statement will do all the looping for you once it is written correctly.
     
  24. Wyatt_Earp

    Wyatt_Earp MajorGeek

    Keep in mind, that JJJIrish is explaining arrays similar to a C-Type language. Not all languages treat the initialization of arrays like this. For example, in VB, when you declare an array you can use Redim([ArraySize]) where ArraySize would be the last element in the array. The array would then go from 0 to ArraySize rather than 0 to ArraySize - 1.

    So, while most languages do treat arrays like the way that JJJIrish and SWario are explaining, keep in mind that it is language specific. So, if you are writing this in pseudo-code, defining the array with a size of 6 isn't necessarily wrong.

    Having said that, I would have to agree that an array size of 7 makes more sense. :)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds