duduk
New Member
Posts: 6
|
Post by duduk on Sept 23, 2018 16:34:02 GMT -5
Hello,
The task goes like this: Enter the dimension and members of the matrix A. In every row by the help of the subroutin find the odd numbers and then make a sum of those numbers in the row and then the number we got from that put in the sequence NEP. After that print matrix A and sequence NEP. Please help if you can.
this is how I started the task, go to attachment.
Attachments:ispitni.bas (571 B)
|
|
|
Post by tsh73 on Sept 24, 2018 2:15:01 GMT -5
Arrays are always global in LB - passing it to sub do nothing Parameters are passed by value by default, so calling call Niz B,S,N does not update "S" so Nep(i)=S will put 0 in each cell. -- there is special byRef keyword
Also sub Niz B,S,N probably needs "S=0" line at very top.
Last thing. For debugging, do replace input "A(";i;",";j;")=";A(i,j) with random values: A(i,j)=int(rnd(0)*10) 'random integer 0..9
edit program could be made working by fixing sub Niz B,S,N - S=0, - byRef S, - and you have "i" and "j" index mixed in there.
|
|
duduk
New Member
Posts: 6
|
Post by duduk on Sept 24, 2018 5:43:34 GMT -5
Yes, you see, I am beginner here in this program, so I need this to pass my exam, I need to enter elements for matrix by myself so random won't do good for me, also If you could help me and write the full program just copy from my attachment or write somehow so I could know where exactly am I wrong. I am not very good at it but I would love to be better here, if you could help me and write the lines of the code offcourse if you have time, but since you are moderator I suposose you have a lot of work. Just this is bothering me for one week and I'm trying my best, also that byRef S, we haven't mention so I don't know how to work with that. Just as I told you at the beggining I am at the very beggining at writing these codes, I'm not that good and please if You can anyway to help me by writing direct code I would be very,very thankfull. Attachments:
|
|
|
Post by Rod on Sept 24, 2018 6:49:57 GMT -5
Well tsh73 is a computer science tutor so he isn't going to write the code for you, he wants to see you work through it.
Me? well have a look at this, it is essentially your code but I moved things around a little. The random numbers just help test out the program quickly. You can put the input line back in if you really need to. So now we get the size of the matrix and then dim the variables we need. I don't think you needed B() I also used meaningful names for the indices, that makes it much easier to follow whats going on. The rem statements also help by explaining what is going on. This will let your teacher know you are understanding what is happening.
'first get the size of the square matrix input "Unesite dimenziju kvadratne matrice A: ";N
'now dimension variables to hold the data dim A(N,N) dim NEP(N)
'fill the matrix with random numbers between 0 and 9 for Row=1 to N for Col=1 to N 'input "A(";Row;",";Col;")=";A(Row,Col) A(Row,Col)=int(rnd(0)*10) 'random integer 0..9 next next
'now sum the odd values in each row for Row=1 to N call Niz Row,N next
'now print out your results for Row=1 to N for Col=1 to N print A(Row,Col);" "; next print "Sum = ";NEP(Row) next
end
sub Niz Row,N 'check all the columns in the row for Col=1 to N if ((A(Row,Col)mod 2)<>0) then 'if it is odd then sum it to NEP NEP(Row)=NEP(Row)+A(Row,Col) end if next end sub
|
|
duduk
New Member
Posts: 6
|
Post by duduk on Sept 24, 2018 12:38:56 GMT -5
The saviour, thank you very much, this helped me a lot.
|
|
|
Post by Rod on Sept 24, 2018 12:47:22 GMT -5
Don't copy my code verbatim, your teacher will know. Code it again using your own words and explanation.
|
|
duduk
New Member
Posts: 6
|
Post by duduk on Sept 29, 2018 7:09:01 GMT -5
Primarly, I want to thank you for giving me help in this task, but also I want to apologise for being too much anoying, but this is the problem that I have now. Mainly, my proffesor,exuse me for being rude, is stupid, I have done this task alone even before I have asked you, but he, like he is so much damn stuborn, for him this task isn't valid ( I am talking about this one that you have sent me, deeply thanks again ). So this is the problem, he wants to this task like I have done it in the first post that I have written, in that attachment ( I will sent it again ). He thinks and he like proffesor knows that this taks needs to be done without putting row or col in the subroutine. So my question is, and believe me or not my mark depends on this hahah, my question is, can you solve this assignment without putting row in the subroutine, like: - call Niz row,N ( he thinks, and believe me I told them about you, and everything that you told me, but still he believs that this task must be solved without putting row in the subroutine) Like he need to solve it like this: input "Unesite dimenziju kvadratne matrice A: ";N dim A(N,N) dim NEP(N)
for row=1 to N for col=1 to N 'input "A(";row;",";col;")=";A(row,col) A(row,col)=int(rnd(0)*10) 'random integer 0..9 next next
for row=1 to N for col=1 to N B(col)=A(row,col) next call Niz B,S,N Nep(row)=S next PRINT for row=1 to N for col=1 to N print A(row,col);space$(2); next print next
print "-----------------"
for row=1 to N print NEP(row);space$(2); next
sub Niz B,S,N
for col=1 to N if ((B(col)mod 2)<>0) then S=S+B(col) end if next
print S ' just cheching if it will sum the numbers
end sub
If you could tell me is it possible in any way to this task without getting row in subroutine and how !! Belive me I am trying for a week, and I dont see solution, so You are my last resort, my last hope. I begg you if you know the way please tell me. Sorry for inconvenience I may have caused You, these are my first forum posts and believe me I don't ask for help a lot, but this time I had to. Thank you. Attachments:ispitni.bas (717 B)
|
|
|
Post by Rod on Sept 30, 2018 8:53:35 GMT -5
I wonder if your tutor is looking for a very simple function to tell whether a value is odd or not? This amended code does not pass Row or Col it passes just the value to check and returns the value if it is odd or 0 if it is even. That allows you to sum the result in the main program.
'first get the size of the square matrix input "Unesite dimenziju kvadratne matrice A: ";N
'now dimension variables to hold the data dim A(N,N) dim NEP(N)
'fill the matrix with random numbers between 0 and 9 for Row=1 to N for Col=1 to N 'input "A(";Row;",";Col;")=";A(Row,Col) A(Row,Col)=int(rnd(0)*10) 'random integer 0..9 next next
'now sum the odd values in each row to NEP() for Row=1 to N for Col=1 to N NEP(Row)=NEP(Row)+Niz(A(Row,Col)) next next
'now print out your results for Row=1 to N for Col=1 to N print A(Row,Col);" "; next print "Sum = ";NEP(Row) next
end
function Niz(x) 'Is x odd? if so return x else return 0 if x mod 2 <>0 then Niz=x else Niz=0 end if end function
|
|
duduk
New Member
Posts: 6
|
Post by duduk on Sept 30, 2018 10:33:10 GMT -5
Well he is very demanding and to be honest he is very old, so he uses his own technicues to prove something. I know that Your codes are much more than good, but he told me to fix the problem in my code, so he wants it to be with subroutine like I have it call Niz B,S,N , so he said like find problem in your code, where it is and do it with call Niz B,S,N. So my problem is that like tsh73 told me call Niz B,S,N does not update "S" when I write it after ( Nep(i)=S ) . So this is mainly the problem I need to fix. I know that you are smarter than him and that this is the way to solve it, but trust me if you had this teacher at any time you will se how blind he is in this way. So just this is my problem I gave him all these choices and he wants it to do it with call Niz B,S,N , all I need is to update S there and I don't have any clue how. If it's possible at any way I really wan't to get rid of him once and for all. Trust me, he is very stuborn. In my code at the last attachment I have sent you, in the subroutine S is printing ok, those are the solutions, now all I need is to transport those solutions from S to NEP() and I must use S and NEP. If you know how to update it I beg you this is frustrating for me like I believe is for you too, and I'm sorry if it causes you a problem, I don't know whom else to ask. No one here is capable to solve it in this way, so I had to ask you, the programers who know know the code.
|
|
|
Post by tsh73 on Sept 30, 2018 11:24:34 GMT -5
Hello duduk Asking for help is normal. People here are like to help others. It's nice you checked that S in your sub is calculated OK Now, just change sub Niz B,S,N to sub Niz B,byRef S,N It should pass S back to caller. Here help page describing byRef www.libertybasicuniversity.com/lb4help/BYREF.htmWait It looks like your program still prints 0's Add "PRINT S" after CALL - you see it is correctly returns from a sub? (I do) But why it didn't work? Hint: you put S into array called ... and in printing you call same array - ?? (LB distinguish case in variable names. So 'x' is different variable from 'X' And your arrays? )
|
|
duduk
New Member
Posts: 6
|
Post by duduk on Sept 30, 2018 13:01:18 GMT -5
Yes I know, but it's hard just because you are giving me right results and I know that, but as I have told you that person is so stuborn, believe me it's hard to satisfy him even when I'm right he want's to be like he wants and I must listen or I won't pass the test. I just need to do this assignment like he want's and I'm done with him.But back to problem. I understand now what that byRef is working and I have fixed all problems like you have mentioned, but still that's problem with him, he won't believe me again if I show him this, he want's it to be like he wants. But the problem is that we haven't even mention "byRef" in lectures, so even he won't know what it is, is there any other way to transfer that S from subroutine to NEP(). Believe me I know you are hell of a proffesonals by me, but he is so damn demanding and thats why I'm always apologising because even when the problem is fixed, for him it isn't fixed. So my question is: Is there any other way to transfer that S to NEP, because we haven't mention at all byRef. Even when I mentioned him that on the last meeting he didn't know that keyword, so believe me when I say, he doesn't know everything in Lib. Basic so he just wants to work with that knowledge that he has. Thats why I'm asking and trying to fix this "problem" .
|
|
|
Post by tsh73 on Oct 1, 2018 10:34:25 GMT -5
I really wonder if here is other BASIC used in your course? Like I said, > Arrays are always global in LB and you say > we haven't even mention "byRef" in lectures So "sub Niz B,S,N" just doesn't make sense that way (no way to pass array in, no way of getting result out). But in some other BASIC - that just might. As for passing S out without byRef - if it could be made, why they added byRef keyword in a first place (in LB)? There is a way of using GLOBAL variable. But then where no need to pass S (ah well, there no need to pass "B" array - and actually no way to - in LB) - and if you leave S in parameters, something goes wrong (and LB has a *bug* right here) And there is a really NORMAL way to passing number out - and it is using FUNCTION instead of SUB. But again it will not be "sub Niz B,S,N" any more.
|
|