Adrian Meneses

Greenhorn
+ Follow
since Oct 04, 2017
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Adrian Meneses

Hello, I'm trying to understand why a member static variable does not compile with the flag -O0 but it does with -O1 and above. A snippet code:


I've been reading about One Definition Rule and it seems that the way SKILL is being used is the issue but I don't quite understand it. Why would the compiler complaint with -O0 but not with -O1? I know static variables should be declared in the cpp file always but I'm trying to understand the difference between -O0 and the other optimization flags. The command I used to compile was:
1 year ago

Tim Holloway wrote:This is kind of gauche to me: "memset(left, '\0', 50)". If, as I presume, you mean for '\0' to stand for the octal byte value \000, I'd recommend that you follow common practice and write all 3 digits.

Although in actual fact, I'd simply code "memset(left, 0, 50)", as "0" is the universal nothing in C, signifying ASCII NUL, floating-point 0.0, and null pointer value (which, interestingly, on some systems is not actually address 0)..

Then again, I think that there may be a standard memset-equivalent that's explicitly intended to deal with character arrays. A character, after all, isn't always exactly one byte in the wider world.

As to getting a binary 1 for pass 1, my first instinct would be to see if the loop index was somehow getting pushed into a character.

But I think your code would probably be clearer (and easier to debug!) if you considered doing that central loop block using a switch statement.


My intention for setting '\0' with memset was because I was out of ideas after trying so many things before (one of those being '0'. I tried to implement the switch statement but at the time was working with arrays that had been malloc'ed. I will give it another shot with memset(left, 0, 50) as to ease your eyes haha!
4 years ago

Campbell Ritchie wrote:Have you verified where that \001 is in you file? Have you verified that all the characters in that file are ≤ 0xff? What is the encoding for your file? If you have any characters ≥ 0x100, or anything non‑ASCII, there is the possibility of an unexpected pairing.


This is a bit out of my scope, not sure what are you asking.
4 years ago
Hello,

I'm facing a problem which I do not understand at all. I'm doing a program which extracts digits from a file. I will cut to the chase:
Every line in the file where I'm extracting the numbers from is as follows: "00:00:01,194 --> 00:00:02,255" which means a dialogue (from a movie/doc) starts at 1 second and 194 thousands and ends at 2 seconds with 255 thousands. My code is the following:


Using GDB, I can see that the first iteration runs fine. Every index of array "left" are properly filled with the digits. The issue comes after the second iteration (when row is 1) because I can see that the array "left" and "right" are cleared out by memset( ) but when it adds the first digit to the "left" array at index 0, index 1 gets populated with hex value 1 which is ASCII for "Start of Header" and I don't understand why. As an example, let's say we are working with the data from arr[1] because we are done with arr[0]:
The "left" array right now looks as follows because memset( ) was just executed:


Now, line 14 gets executed and is the digit '0', the if-else statements are checked and line 21 is executed... the "left" array now looks as follows:


Next character is also a '0', if-else statements are checked and the digit is added. Now the array is as follows:


The file (an .srt file) that I read with a FILE pointer:
   

Hope this helps!
4 years ago

Ron McLeod wrote:

Adrian Meneses wrote:Does not print even though it compiles and runs just fine. I can enter lines of text but it will not print how many lines have been entered. What is wrong?


The code is going to stay ion the while loop until there is no more input - how are you terminating your input?  From the Linux command line, you would normally use Control-D:
You could also test by redirecting standard input from a file:


Thank you! I am not familiar with the EOF in the library and because of that I was terminating the program with Control-C.
6 years ago
Hello again and Happy New Year 2019!

I am learning C from the book "The C Programming Language" second edition by Brian Kernighan and Dennis Ritchie. There are a couple examples (character and line counting) that do not work for me when I code them. For example, line counting is:



Does not print even though it compiles and runs just fine. I can enter lines of text but it will not print how many lines have been entered. What is wrong?
6 years ago
Thank you Junilu! What an oversight from my part! Still learning how queues work with nodes.
7 years ago

Adrian Meneses wrote:Hello all,
I'm trying to print the contents of a queue using nodes but I get duplicates for some reason and I don't understand why. Here is my code:




The unbounded interface just overrides a queue interface where the enqueue, dequeue, size, and toString methods are. The console prints the last item added to the queue. For example, if I add Mary with ID 10, Jon with ID 20, and Adrian with ID 30, the program will print:
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30

Please help me by pointing where the error/bug is, tell me in a conceptual way so I see what's wrong and to code it.  


I added the do-while loop in the demo class that I forgot to post. The program, as I see it, adds a new student to the queue but when it comes to print all the students in the queue it only prints the last student added multiple times.
7 years ago
Hello all,
I'm trying to print the contents of a queue using nodes but I get duplicates for some reason and I don't understand why. Here is my code:




The unbounded interface just overrides a queue interface where the enqueue, dequeue, size, and toString methods are. The console prints the last item added to the queue. For example, if I add Mary with ID 10, Jon with ID 20, and Adrian with ID 30, the program will print:
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30

Please help me by pointing where the error/bug is, tell me in a conceptual way so I see what's wrong and to code it.  
7 years ago

Campbell Ritchie wrote:Two more things:

  • 1: Before popping something it will be necessary to null out the location in the array, otherwise you are risking a hard reference to an object no longer required.
  • 2: As an alternative to throwing an exception when the array is full, it is possible to copy the entire contents into a larger array.

  • I caught that too, where the element is not removed or deleted from the array. A bit confusing at the moment for me to see array[size] instead of array[index] but it will get better with time. Also, Junilu coded, on line 27, elements[size++] = element; but since the array defaults at 0, wouldn't that put the element at index 1 instead of 0? Or when he comments that the size of the array defaults to 0 he means that is defaults to null?
    7 years ago

    Junilu Lacar wrote:A couple of other things:

    1. Your push() logic looks faulty to me. Seems like you would get an ArrayIndexOutOfBoundsException on the very first call to push() a Book unto a new Stack Never mind, I see you did the increment and use of the index in two different statements. That's not how it's normally coded though. You wouldn't initialize the index of a new stack to -1 but 0. Then do the post increment at the same time you're accessing your stack array.

    2. You declared the myBookStack variable as Stack not StackInterface. What's the point of declaring and implementing StackInterface if you're not going to use that abstraction?


    Hi again!
    I asked my professor as to why we are initializing the index as -1 and he said it could be initialized as 0 or any other number as long as I keep track of the index I am working. Regarding the implementation of myBookStack instead of StackInterface is because we are taking it slow, the class in beginning to see why we should use ADT. More importantly, I now understand why I need to declare an Interface in the demo class.

    Finally, I have a question about printing queues with nodes (using the toString() method) but should I open another thread? And is there a way to mark this one as solved?
    7 years ago
    I can't figure out where the problem is to push() and print the top() of the stack (or peek() as it is known). This is, in order, my code:
    This is the interface:

    This is my Stack class that implements the interface:

    This is the Book class

    This is the book demo class:
    7 years ago

    Campbell Ritchie wrote:That looks as though you need a way of enumerating languages. How much do you know about enumerated types? There is a lot in the Java® Language Specification about them, too.


    I didn't know about that data type. I read about it and it might be useful for some applications. Are enum types only Strings?
    7 years ago

    Knute Snortum wrote:You don't need parameterization to do what you're describing, just three instances of MyStack.  If you wanted to -- and it might be a good idea -- you could parameterize MyStack so that it could hold Books or Clocks or Chickens or whatever, but you don't need to do that to have a stack of English, Spanish, and Other language Books.


    I think I have to use the parameterize stack to learn how it works. And as you said it, I can put books, chickens, or clocks but the example I gave was red book, blue book, and green book. Thank you for the help. I solved that problem and ran into another one but I want to try and solve it on my own. If I can't, I'll ask for some guidance.
    7 years ago

    Knute Snortum wrote:Why is the class Book parameterized?  What would T be for a Book?  Do you use T in the Book Class?  What does BookInterface look like?  Why do you need to implement it in MyStack?


    I know you guys help a lot but you don't solve assignments or homework for people that have issues. The assignment read that there is a pile of books with many authors and some are written in English while other books are written in Spanish. The rest, are written in another language. I have to create an Interface, in this case BookInterface, that will be implemented by the MyStack class where the methods push(), pop(), and peek() will be. Then, the Book class will have the setAuthor(), setLanguage() with the toString() method. after that, I have to ask the user to enter the information from the first book (let's say the author is Charles Dickens and is in English) then I will use this information to put the first book from the pile in the stack of English books. If the information from the second book says that the author is Pablo and the book is written in Spanish, then the book will be placed in the Spanish stack. If the third book is by Hamil and is written in Portuguese, then the Portuguese stack will be used. <T> would be Strings? I can post my code from the Interface, Book, and Demo so you can see what I'm doing or trying to do.
    7 years ago