• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Thanks, will look into it (although I'm not sure I really need anything fancier than a makefile... even my thesis compile on a makefile with two dozen lines...)

While you might be right, the best way to start learning new things is to just start using them. In this day and age, frankly i think Make should just be end of lifed. Everything it does, it does worse than other technologies. That's not the kind of thing you want to cling to if you can avoid
 
I'm currently working on learning how to code device drivers for my job. I got a Hello World kernel module (hello.ko) to run on the company's latest device, but there's still a long way to go before I can tackle the current assigned project. What kind of books and topics do you guys recommend I read, and what kind of practice programs should I write?

Currently I'm reading Linux Device Drivers.

I am very familiar with C++ and Java, but have never coded in C itself. I know C++ is a superset of C, but there do seem to be some differences, such as how structs are declared and implemented. Should I be worried about this disparity in knowledge?
 

Water

Member
That's interesting, at least half of all the high schools around here use C# for high school CS, the other half uses Java (my HS had java). I'm enjoying it with Unity so far, it's a nice language.

Weird. I've never heard of C# being used for introductory programming education or really any kind of computer science at a university (except for ultra-niche purposes like my own where it's the language of choice due to Unity). C# also doesn't seem to attract the kind of inspiring, super-smart people from outside academia that eg. C++ has forming a visible expert community. The resulting lower quality and quantity of materials compared to other mainstream languages seems like something that would specifically discourage high school level institutions from using it. It's not like they have enough programming student/teaching volume, resources or competence to justify blazing their own path vs using existing high quality materials. And there are so much better teaching languages to choose from.
 
Latest assignment for Python 3, involves arrays of one dimension:



Help, as usual, would be greatly appreciated.

Is the "showStats" module already defined and you are just using it? Or are they asking you to create it with those functions?

As for the program itself... well like a lot of people have been saying you should look over your notes or previous programs to find some ideas.

I suppose you can use a list to store the names of the months and then iterate through that with a for loop. In the for loop ask for input and store that input in a second list called "values" or something. (Or you could use a dictionary)

Once you have all of the user input for the rainfall it's just a case of case of finding things like the maximum or minimum which may already be defined for you by that module but shouldn't be too hard to find yourself.

I recommend reading up on Lists in general, their functions and maybe some tutorials to get a proper feel for what you can do with them. There is a ton of info online, some of it well worded and easy to understand and some of it a bit more complex and jargon filled.

If you just keep going through the problem slowly one small step/problem at a time you should get the answer. Divide and conquer the problem into smaller pieces.
 

SOLDIER

Member
Saying that the assignment is basic doesn't make me feel any better, as I continue to struggle nailing down even the most basic steps. I don't think the professor does a good job dumbing it down for students (the ones I have spoken to echo my frustration), and the provided compendium is either incorrect or I'm missing something.

So this is what I have so far:

Code:
def main():
    rainfall = [0, 0, 0]
    high_rainfall = 0.0
    low_rainfall = 0.0
    total_rainfall = 0.0
    average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

print(rainfall[0])
    
main()

After it asks me to enter the rainfall for January, it spits out an error saying "rainfall is not defined".
 
Saying that the assignment is basic doesn't make me feel any better, as I continue to struggle nailing down even the most basic steps. I don't think the professor does a good job dumbing it down for students (the ones I have spoken to echo my frustration), and the provided compendium is either incorrect or I'm missing something.

So this is what I have so far:

Code:
def main():
    rainfall = [0, 0, 0]
    high_rainfall = 0.0
    low_rainfall = 0.0
    total_rainfall = 0.0
    average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

print(rainfall[0])
    
main()

After it asks me to enter the rainfall for January, it spits out an error saying "rainfall is not defined".

Because you defined "rainfall" in your function the list variable "rainfall" is local to that function. You are trying to call it then outside the function where it doesn't exist.

Instead of using a "main" function at all for the moment just put everything at the same level of indentation and then your code should work.

So if you do it like this:

Code:
rainfall = [0, 0, 0]
high_rainfall = 0.0
low_rainfall = 0.0
total_rainfall = 0.0
average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

print(rainfall[0])

If you do it like this where it is all at the same level of indentation then for Python that means it is all part of the same code block.
 

Water

Member
I am very familiar with C++ and Java, but have never coded in C itself. I know C++ is a superset of C, but there do seem to be some differences, such as how structs are declared and implemented. Should I be worried about this disparity in knowledge?
If you really are very familiar with C++, you should be able to write C just fine, but many things C++ has handed to you on a silver platter now don't exist or have to hacked together painfully. Want a generic datatype? No templates in C, but you can use C preprocessor macros to process code to generate versions of the same code for different types. Want a "class" whose contents can't be directly messed with by code outside the class? You can hide the struct and its "method" definitions in their own compilation unit and still let outside code manipulate the structs because the function prototypes only ever contain pointers to the struct instead of struct values.

C++ isn't an exact superset of C, they have diverged a bit with stuff like C's variable length arrays that do not exist in C++. The more fine-grained your knowledge of C++ is, the more differences you'll see - I believe aspects like concurrency model are significantly different between modern C++ and C, but I've never had to mess with that stuff.

What C++ are you familiar with, and what C are you going to write now? C++11 and C99?
 

Koren

Member
While you might be right, the best way to start learning new things is to just start using them.
I agree, and I already use Cmake, I just haven't look at how to use tex with it. I'll definitively try it, out of curiosity.

I'm not sure it'll be simpler or better for my usage, though.


In this day and age, frankly i think Make should just be end of lifed. Everything it does, it does worse than other technologies.
About this, I'm not so sure. You can set up a compulation rule with two lines, or even not a single line with noemal rules. It still has the simplicity for it. I don't see a need to get rid of it. Restrict its usage to basic tasks, on the other hand...

Is there anything that Bash still has conpared to Scala, Python for scripts? I don't believe it's a sufficient reason to kill shell scripts...
 

peakish

Member
Saying that the assignment is basic doesn't make me feel any better, as I continue to struggle nailing down even the most basic steps. I don't think the professor does a good job dumbing it down for students (the ones I have spoken to echo my frustration), and the provided compendium is either incorrect or I'm missing something.

So this is what I have so far:

Code:
def main():
    rainfall = [0, 0, 0]
    high_rainfall = 0.0
    low_rainfall = 0.0
    total_rainfall = 0.0
    average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

print(rainfall[0])
    
main()

After it asks me to enter the rainfall for January, it spits out an error saying "rainfall is not defined".
As Ruyjin points out, there are two things going on here.

The first is that you are declaring the array rainfall inside of a function and trying to use it outside of it. Think of your indentation as a level: Code that is indented one level can see objects in the previous level, but not vice versa. This is called scope.

The second is order of execution. Your program will step through the three input(...) lines and only then reach the main() function, at which point the program would enter it and make the declaration. So even if the program could read rainfall from within the function (it can't), your program would try to use the array before declaring it which would not be allowed.

I also agree with Ryujin: Try to write the program without using a function, then clean it up by moving code into functions after you are finished. It sucks to have a bad lecturer, but there's plenty of online material available which should help you complement the lectures. I'd recommend Learn Python the Hard Way, with the caveat that it uses Python 2 while you might be running Python 3. There are some key differences, but it should still teach you the concepts of the language. There's also Code Academy which has a Python introduction.
 

SOLDIER

Member
This whole thing probably would be a lot easier to understand without that main() function in the way.

Anyway, I'm going through it, and I believe I already know how to get the total and average. I just need to figure out how to have it pick the highest number and the lowest number (for High rainfall and Low rainfall).
 
This whole thing probably would be a lot easier to understand without that main() function in the way.

Anyway, I'm going through it, and I believe I already know how to get the total and average. I just need to figure out how to have it pick the highest number and the lowest number (for High rainfall and Low rainfall).

There are list functions min(list) and max(list) which return the minimum or maximum element of a list. So if you have a list like so:

Code:
list = [1,2,3,4,5,6]

Then by using max(list) like so:

Code:
highest_number = max(list)

# Here 'highest_number' will be equal to 6 which is the highest or 'maximum' value of the list

This is the exact way with min(list) but obviously taking the lowest value item in the list.
 

SOLDIER

Member
Yeah, I found out about max right away, but now there's a new error:

Code:
rainfall = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
high_rainfall = 0.0
low_rainfall = 0.0
total_rainfall = 0.0
average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

rainfall[3]=int(input("Enter the rainfall for April: "))

rainfall[4]=int(input("Enter the rainfall for May: "))

rainfall[5]=int(input("Enter the rainfall for June: "))

rainfall[6]=int(input("Enter the rainfall for July: "))

rainfall[7]=int(input("Enter the rainfall for August: "))

rainfall[8]=int(input("Enter the rainfall for September: "))

rainfall[9]=int(input("Enter the rainfall for October: "))

rainfall[10]=int(input("Enter the rainfall for November: "))

rainfall[11]=int(input("Enter the rainfall for December: "))

print("Average rainfall is: "(max(rainfall)))

Max will display the number fine, but once I add that print function the error says "print("Average rainfall is: "(max(rainfall)))
TypeError: 'str' object is not callable"
 
Yeah, I found out about max right away, but now there's a new error:

Code:
rainfall = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
high_rainfall = 0.0
low_rainfall = 0.0
total_rainfall = 0.0
average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

rainfall[3]=int(input("Enter the rainfall for April: "))

rainfall[4]=int(input("Enter the rainfall for May: "))

rainfall[5]=int(input("Enter the rainfall for June: "))

rainfall[6]=int(input("Enter the rainfall for July: "))

rainfall[7]=int(input("Enter the rainfall for August: "))

rainfall[8]=int(input("Enter the rainfall for September: "))

rainfall[9]=int(input("Enter the rainfall for October: "))

rainfall[10]=int(input("Enter the rainfall for November: "))

rainfall[11]=int(input("Enter the rainfall for December: "))

print("Average rainfall is: "(max(rainfall)))

Max will display the number fine, but once I add that print function the error says "print("Average rainfall is: "(max(rainfall)))
TypeError: 'str' object is not callable"

So what is happening here is that rainfall is an 'int' type variable and "Average rainfall is:"
is of type "str" (string).

What you need to do is convert max(rainfall) into a string like so:

Code:
str(max(rainfall))

Now the second thing is that you can't just put the variables in there like that to the print function you should use the format print(string1 + string2). The key part here being the '+' symbol which when used with strings combines them together into a new string.

So your print code should look like this:

Code:
print("Average rainfall is: " + str(max(rainfall)))

The second way to do it is that you don't convert to a string at all and you seperate the two variables with a comma ' , ' character like so.

Code:
print("Average rainfall is: ", max(rainfall))
 

SOLDIER

Member
Sorry, I messed up that last line. It's actually the highest rainfall I was looking for, but I figured that out.

Code:
rainfall = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
high_rainfall = 0.0
low_rainfall = 0.0
total_rainfall = 0.0
average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

rainfall[3]=int(input("Enter the rainfall for April: "))

rainfall[4]=int(input("Enter the rainfall for May: "))

rainfall[5]=int(input("Enter the rainfall for June: "))

rainfall[6]=int(input("Enter the rainfall for July: "))

rainfall[7]=int(input("Enter the rainfall for August: "))

rainfall[8]=int(input("Enter the rainfall for September: "))

rainfall[9]=int(input("Enter the rainfall for October: "))

rainfall[10]=int(input("Enter the rainfall for November: "))

rainfall[11]=int(input("Enter the rainfall for December: "))

high_rainfall = (max(rainfall))

print ("The highest rainfall is: " +str(high_rainfall))

low_rainfall = (min(rainfall))

print ("The lowest rainfall is: " +str(low_rainfall))

Almost done, I just need to figure out how to put this all together through main(), which is always what messes me up.
 
Sorry, I messed up that last line. It's actually the highest rainfall I was looking for, but I figured that out.

Code:
rainfall = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
high_rainfall = 0.0
low_rainfall = 0.0
total_rainfall = 0.0
average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

rainfall[3]=int(input("Enter the rainfall for April: "))

rainfall[4]=int(input("Enter the rainfall for May: "))

rainfall[5]=int(input("Enter the rainfall for June: "))

rainfall[6]=int(input("Enter the rainfall for July: "))

rainfall[7]=int(input("Enter the rainfall for August: "))

rainfall[8]=int(input("Enter the rainfall for September: "))

rainfall[9]=int(input("Enter the rainfall for October: "))

rainfall[10]=int(input("Enter the rainfall for November: "))

rainfall[11]=int(input("Enter the rainfall for December: "))

high_rainfall = (max(rainfall))

print ("The highest rainfall is: " +str(high_rainfall))

low_rainfall = (min(rainfall))

print ("The lowest rainfall is: " +str(low_rainfall))

I just need to get the total rainfall and the average. Pretty sure I know how to get the average, just need to get the total from the list.

Also I need to figure out how to put this all together through main(), which is always what messes me up.

Lists have a built in function much like min() and max() called sum(list). This will give the sum of all elements in the list.

I highly recommend looking more into lists when you get a chance as all of these functions are explained and lists are really handy in Python.

As for the 'main' function all you have to do is tab all your variables by 1 (or use 4 spaces). Then just wrap your finished code in 'def main():' tag at the top.

Then at the very end outside the function at the same level of indentation as 'def main():"
just type 'main()' this will call or 'use' the function.
 

SOLDIER

Member
Okay, almost done. Just need to get it working in main()

Code:
rainfall = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
high_rainfall = 0.0
low_rainfall = 0.0
total_rainfall = 0.0
average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

rainfall[3]=int(input("Enter the rainfall for April: "))

rainfall[4]=int(input("Enter the rainfall for May: "))

rainfall[5]=int(input("Enter the rainfall for June: "))

rainfall[6]=int(input("Enter the rainfall for July: "))

rainfall[7]=int(input("Enter the rainfall for August: "))

rainfall[8]=int(input("Enter the rainfall for September: "))

rainfall[9]=int(input("Enter the rainfall for October: "))

rainfall[10]=int(input("Enter the rainfall for November: "))

rainfall[11]=int(input("Enter the rainfall for December: "))

high_rainfall = (max(rainfall))

print ("The highest rainfall is: " +str(high_rainfall))

low_rainfall = (min(rainfall))

print ("The lowest rainfall is: " +str(low_rainfall))

total_rainfall = (sum(rainfall))

print ("The total rainfall is: " +str(total_rainfall))

average_rainfall = (sum(rainfall)/len(rainfall))

print ("The average rainfall is: " +str(average_rainfall))
 
Is there a website where I can learn how to code for visual studio in C#?

Code what in C#? Windows desktop apps on a particular type of UI? Windows mobile apps? Some kind of web stuff? Unity games? Visual Studio is just a development environment you can use while developing for any of these platforms and frameworks. C# beginner materials are scarce, and most of it is targeted towards one of these specifically. People tend to only learn C# for a specific purpose after they already have programming experience in something else, it is not popular at all as a teaching language. I'm one of the rare people who teach introductory programming with it, but that's because I have to get the students into Unity game development ASAP, can't afford to use a separate teaching language first.


Code very basic things, like calculating Pizza, if statements, loop statements, declaring variables, doing the math

the very beginning.
 
Yeah, I found out about max right away, but now there's a new error:

Code:
rainfall = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
high_rainfall = 0.0
low_rainfall = 0.0
total_rainfall = 0.0
average_rainfall = 0.0

rainfall[0]=int(input("Enter the rainfall for January: "))

rainfall[1]=int(input("Enter the rainfall for February: "))

rainfall[2]=int(input("Enter the rainfall for March: "))

rainfall[3]=int(input("Enter the rainfall for April: "))

rainfall[4]=int(input("Enter the rainfall for May: "))

rainfall[5]=int(input("Enter the rainfall for June: "))

rainfall[6]=int(input("Enter the rainfall for July: "))

rainfall[7]=int(input("Enter the rainfall for August: "))

rainfall[8]=int(input("Enter the rainfall for September: "))

rainfall[9]=int(input("Enter the rainfall for October: "))

rainfall[10]=int(input("Enter the rainfall for November: "))

rainfall[11]=int(input("Enter the rainfall for December: "))

print("Average rainfall is: "(max(rainfall)))

Max will display the number fine, but once I add that print function the error says "print("Average rainfall is: "(max(rainfall)))
TypeError: 'str' object is not callable"

Err.. Have you learned about loops yet?
 
Err.. Have you learned about loops yet?

I don't think he has so far, he seems to have a bad lecturer who doesn't explain how things work in a jargon free way.

I would have mentioned that to him but I think just getting the basic version of the program working is more important for him at the moment.
 

SOLDIER

Member
Loops have come up at some point, yes, but it's yet another element I haven't wrapped my head around.

Frankly I'm happy I got this working in the end:

Code:
def main():
    rainfall = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    high_rainfall = 0.0
    low_rainfall = 0.0
    total_rainfall = 0.0
    average_rainfall = 0.0

    rainfall[0]=int(input("Enter the rainfall for January: "))

    rainfall[1]=int(input("Enter the rainfall for February: "))

    rainfall[2]=int(input("Enter the rainfall for March: "))

    rainfall[3]=int(input("Enter the rainfall for April: "))

    rainfall[4]=int(input("Enter the rainfall for May: "))

    rainfall[5]=int(input("Enter the rainfall for June: "))

    rainfall[6]=int(input("Enter the rainfall for July: "))

    rainfall[7]=int(input("Enter the rainfall for August: "))

    rainfall[8]=int(input("Enter the rainfall for September: "))

    rainfall[9]=int(input("Enter the rainfall for October: "))

    rainfall[10]=int(input("Enter the rainfall for November: "))

    rainfall[11]=int(input("Enter the rainfall for December: "))

    high_rainfall = (max(rainfall))

    print ("The highest rainfall is: " +str(high_rainfall))

    low_rainfall = (min(rainfall))

    print ("The lowest rainfall is: " +str(low_rainfall))

    total_rainfall = (sum(rainfall))

    print ("The total rainfall is: " +str(total_rainfall))

    average_rainfall = (sum(rainfall)/len(rainfall))

    print ("The average rainfall is: " +str(average_rainfall))

main()

I just hope the professor likes it.
 

Makai

Member
I assume you haven't learned methods yet. I managed to make a Smash Bros clone in XNA before I learned classes, methods, or even loops. Learning those was like being given an ancient weapon from the programming gods.
 

Droplet

Member
Loops have come up at some point, yes, but it's yet another element I haven't wrapped my head around.

Frankly I'm happy I got this working in the end:

I just hope the professor likes it.

I guess the nature of the assignment doesn't really give you much to work with in terms of shrinking this down, but at the very least you don't need to assign your variables until you use them. So you could just get rid of all the high_rainfall = 0.0 etc at the beginning, since you assign them later on.
 
Not a programming issue, just wanted to vent.

Dear Apple, please add a damn white caret cursor option for dark XCode themes. We've been asking for this since like XCode 4. About tired of doing it myself damn it! :3
 

poweld

Member
Loops have come up at some point, yes, but it's yet another element I haven't wrapped my head around.

Frankly I'm happy I got this working in the end:

I just hope the professor likes it.

For future reference, here's a way you could solve this with a loop:
Code:
months = [
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December"
]

for index, month in enumerate(months):
  rainfall[index] = int(input("Enter the rainfall for %s: " % month))

The idea is to not repeat yourself while writing code, if at all possible. By looping through the months, we can pose the same question substituting the month name and placing the answer in each successive index in the rainfall array.
 
For future reference, here's a way you could solve this with a loop:
Code:
months = [
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December"
]

for index, month in enumerate(months):
  rainfall[index] = int(input("Enter the rainfall for %s: " % month))

The idea is to not repeat yourself while writing code, if at all possible. By looping through the months, we can pose the same question substituting the month name and placing the answer in each successive index in the rainfall array.

You could get the month names from calendar.month_name too :p
 

Koren

Member
You could get the month names from calendar.month_name too :p
And use a list comprehension instead of an enumerate and a mutation ^_^

Full program in three lines:
Code:
rainfall = [ int(input('Enter the rainfall for '+str(month)+': ')) for month in calendar.month_name[1:] ]

for func, name in [ (max, 'high'), (min, 'low'), (sum, 'total'), (lambda x : round(sum(x)/12., 4), 'average') ] :
    print(name, 'rainfall is:', fun(rainfall))

OK, I'm having fun with the second part... But Poweld said *no repetition* ^_^


About calendar, I really don't like that there's an empty string at the beginning of the list, I keep forgetting it...
 
And use a list comprehension instead of an enumerate and a mutation ^_^

Full program in three lines:
Code:
rainfall = [ int(input('Enter the rainfall for '+str(month)+': ')) for month in calendar.month_name[1:] ]

for func, name in [ (max, 'high'), (min, 'low'), (sum, 'total'), (lambda x : round(sum(x)/12., 4), 'average') ] :
    print(name, 'rainfall is:', fun(rainfall))

OK, I'm having fun with the second part... But Poweld said *no repetition* ^_^


About calendar, I really don't like that there's an empty string at the beginning of the list, I keep forgetting it...

Ok so the list comprehension part I understand, although I've never thought to put an input function there.

In terms of the for loop, from what I can make out you have a list filled with tuples. Each tuple consists of a function at index 0 and a string at index 1.

You are using the 2 variables with the for loop to iterate through the list/tuples: func and name.

At the end you are using lambda which as far as I understand it is a one off un named on the fly function. I don't know much about lambda but the reason you are using this is because there is no build in "avg" function in Python for lists so you are creating your own.

Within lambda you are using the "round" function and rounding to 4 places while dividing the sum of 'x' by 12 (the number of months). And then 'x' is a temporary function variable/parameter that lambda is performing the operation on...

So when you say

Code:
func(ranfall)

You are performing the lambda function on the rainfall list because func = the lambda function in the tuple (at this point in iteration) and rainfall is 'x' in this case...

Am I right with my breakdown or am I missing anything? I've never really used lambda before and know very little about it.
 

Slavik81

Member
Thanks, will look into it (although I'm not sure I really need anything fancier than a makefile... even my thesis compile on a makefile with two dozen lines...)
Mind posting it? I tried using make, but I switched to CMake because I was confused by the need to reprocess the same file repeatedly to fix my references.

I now use CMakeLatex. For simple documents, you don't need the awk scripts. You just drop UseLATEX.cmake into your project folder, and set up your CMakeLists.txt. I did a report a little while ago, and my CMakeLists.txt looked like this:

Code:
cmake_minimum_required(VERSION 2.8)
project(report NONE)
include(UseLATEX.cmake)

set(TEX_SOURCES
  report.tex
)

set(IMAGE_SOURCES
  light_rays_teaser.jpg
  sph-marching-cubes.png
  raycast-through-water.png
  focus-0.png
  focus-2.png
  focus-4.png
  focus-6.png
  focus-8.png
  volume_caustics.png
  surface_caustics.png
  no_caustics.png
)

add_latex_document(
  ${TEX_SOURCES}
  IMAGES ${IMAGE_SOURCES}
  BIBFILES report.bib
)

For the first build:
Code:
mkdir build
cd build
cmake ..
make
For subsequent builds:
Code:
make
Or use ninja with "cmake -G Ninja .." and call ninja rather than make.
 

Koren

Member
At the end you are using lambda which as far as I understand it is a one off un named on the fly function. I don't know much about lambda but the reason you are using this is because there is no build in "avg" function in Python for lists so you are creating your own.
That's correct.

Except that actually, there *is* available 'average' function in Python, for example numpy.mean. But:
- it's actually, for some strange reason, slower than sum on a Python list (*)
- it adds an import
- it's an opportunity to put an example with a lambda, even if Python creator isn't fond of lambda, which I can understand in many cases

(*) it's possible that numpy.mean does the same kind of optimization as math.fsum to avoid cancellation errors, or something like this, I should check, there's probably a reason for this


Am I right with my breakdown or am I missing anything? I've never really used lambda before and know very little about it.
You're perfectly right...

Code:
lambda x : round( (sum(x) / 12.), 4 )

is the same as declaring
Code:
def my_function(x) :
    return round( (sum(x) / 12.), 4 )

and then using my_functions as the first item of the last tuple.

It's just you avoid declaring a function for a single use.


Mind posting it?
I don't mind, but I can't do it know, since I don't have access to the sources at the moment.

But at the same time, I'm not sure it would be so usable anyway, because with bibtex and references, it's always tricky, so it was taylored to this specific document (even more so since I wanted to have a short table of contents at the beginning and a complete one at the end, which LaTeX doesn't like at all), and not a general solution.
 
Honestly, I started to feel that pressure to get expose to a bunch of technologies, but I prefer to know something very well than to just know a little bit just to put on a resume.

I gotta brush up on my notes because it's been a year since I worked with it, but I would be down to work with Android.

The class i took went over activities, list views, intents, basic UI, and a few other things. Would this basic knowledge be enough to be a developer?

Well I'm not in charge of the Android stuff so its hard for me to say for sure, but I could ask our Android guy. Check out our main Android app at www.GetFacer.com and see if its the kind of thing you think you could/would be interested in working on as that is what they're primarily hiring for.

I've wanted to learn c++ on my own time for more low level practice, but rust looks interesting. Are people using rust in the industry yet or is it just a fashionable enthusiast language for now?

Edit: I mean industry in the broadest sense.

I don't know of a lot of people using Rust yet. I'm sure there are places that are doing so but they tend to be more experimental organizations. I know Rust enthusiasts really want to over take C++ in terms of working on embedded systems but who knows if/when that will happen.

hey software engineering gaf. I am looking to expand my skill set, and was looking for opinions on what y'all think is valuable. i've done mostly android/ios/.net, with some ruby on rails. also majored in comp sci, so I think I have a good foundation to start delving into anything. I mainly want to future proof my skill set.

I'm pretty sure it is impossible to future proof your skillset unless you are a Cobol or Fortran programmer. But it is always good to look at trends and changes in whatever industry you work in and keep up on them. A place I used to work laid off a bunch of dudes who hadn't bothered to learn anything after Flash/ActionScript because there was no work for them.

The best advice I can give is unless you're AMAZING at one area its better to not be an X programmer or Y programmer and just be a good software engineer who can learn new things rapidly.
 

Kelsdesu

Member
Hey CodeGaf. I need you assistance with some basic Ruby.

print "Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: "

gets.to_i

def semester
fall = 0
winter = 1
spring = 2
summer = 3

end
puts semester

What I want to happen is that my selection prints at the end. But sadly it does nothing after the first 'gets' command.
Any help would be appreciated.
 

MiszMasz

Member
Hey CodeGaf. I need you assistance with some basic Ruby.

print "Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: "

gets.to_i

def semester
fall = 0
winter = 1
spring = 2
summer = 3

end
puts semester

What I want to happen is that my selection prints at the end. But sadly it does nothing after the first 'gets' command.
Any help would be appreciated.

I've never used Ruby, but your program doesn't appear to do anything after the 'gets', because it doesn't look like you've told it to. Your function and final 'puts' don't seem to do anything.

You need to:
- Store the value you get from your 'gets'.
- Pass it as an argument to your semester function.
- Conditionally match your passed argument to one of your semester names (currently, all your function does is assign a few integers that aren't ever used).
- Return that semester name or print it as the last step of the function.
 

hateradio

The Most Dangerous Yes Man
Hey CodeGaf. I need you assistance with some basic Ruby.

print "Is this Fall[0], Winter[1], Spring[2] or Summer[3] session: "

gets.to_i

def semester
fall = 0
winter = 1
spring = 2
summer = 3

end
puts semester

What I want to happen is that my selection prints at the end. But sadly it does nothing after the first 'gets' command.
Any help would be appreciated.
http://ruby-doc.org/docs/Tutorial/part_02/user_input.html

Code:
userInput = gets.chomp.to_i

Also, you assigned values to the seasons but you didn't even check which value the user put and which season the value belonged to.
 

Koren

Member
I would write it like that, so that it keeps some readability...

Code:
void Switch(char* str) {
    for(; (*str) != 0; ++str) {
        if (((*str)>='A') and ((*str)<='Z'))
            *str += 'b'-'A';
        else if (((*str)>='a') and ((*str)<='z'))
            *str += 'B'-'a';
    }
}

*str += 'b'-'A' means that 'A' becomes 'b', 'B' becomes 'c', etc. ('Z' becomes '{', though, is that OK? If you want 'A', you can add a specific test)

'b'-'A' is computed at compilation time, so it won't slow down the program...


Edit: and if you don't care about readability, just for the fun :

Code:
void Switch(char*s){for(;*s;++s)if((((*s)&192)==64)&&((*s)&31)&&(((*s)&31)<27))(*s)=((*s)^32)+1;}
 
I would write it like that, so that it keeps some readability...

Code:
void Switch(char* str) {
    for(; (*str) != 0; ++str) {
        if (((*str)>='A') and ((*str)<='Z'))
            *str += 'b'-'A';
        else if (((*str)>='a') and ((*str)<='z'))
            *str += 'B'-'a';
    }
}

Yikes, that took me a while of staring to figure out what it meant. I would do something like this instead:

Code:
const char *map_to_lower = "bcdefghijklmnopqrstuvwxyza";

void Switch(char *str) {
   while (*str != 0) {
      if (!isalpha(*str))
         continue;

      char ch = tolower(*str);
      char mapped = map_to_lower[ch - 'a'];
      // If it was originally upper, the mapped value is already the opposite case.
      // If it was originally lower, we need to upper it to get the case change.
      if (islower(*str))
         mapped = toupper(mapped);
      *str = mapped;
      ++str;
   }
}

This also nicely handles the case of Z -> A
 

Koren

Member
Yikes, that took me a while of staring to figure out what it meant.
Really?

Which part, exactly?

Code:
('a' <= ch) && (ch <= 'z')
doesn't seems an especially complex way to check whether it's a lower case character...

It's just the translation part?


For people wondering,
Code:
ch - 'a'
is just a way to get the position of a lower case character 'ch' in the alphabet (it's also used in cpp_is_king example)

and
Code:
'a' + ord
a way to transform an integer 'ord' between 0 and 25 into a lower case character...

would
Code:
(*str) = 'B' + ( (*str) - 'a' )
have been clearer?



I wanted to avoid using any library, since I don't know what he can use...

But even besides this, I don't think a map is more easy to understand.

Do you prefer
Code:
for(;*str;++str) {
    if ( islower(*str) )
        (*str) = 'A' + ( ( (*str) - 'a' + 1 ) %26 );
    else if  ( isupper(*str) )
        (*str) = 'a' + ( ( (*str) - 'A' + 1 ) %26 );
}
?

(and that handle the Z->A, it's just that I didn't know what he wanted to do with Z)
 

Koren

Member
Hey guys, would anyone be able to aid me with a MIPS assignment? I feel like I'm going to rip my hair out.
Assembly?

It's a bit to late for me tonight, but I may have time tomorrow, if you haven't found help inbetween (if it doesn't take too much time to type). Depends on the difficulty of the problem, though, because my MIPS assembly is probably rustier than other variants of ASM :/

I would do something like this instead:
Just a detail... I wanted to try how much slower* it becomes when you use string function (and maybe cache miss with the const char table), out of curiosity, since it's most probably not an issue.

Code:
      if (!isalpha(*str))
         continue;

should probably be
Code:
      if (!isalpha(*str)) {
         ++str;
         continue;
      }


(*) it's four times slower, btw, but should you really need speed, XORing the 0x20 bit of the character is probably even faster
 

Kalentan

Member
Assembly?

It's a bit to late for me tonight, but I may have time tomorrow, if you haven't found help inbetween (if it doesn't take too much time to type). Depends on the difficulty of the problem, though, because my MIPS assembly is probably rustier than other variants of ASM :/

I don't believe it's ASM.

Here is my assignment from my class. All the other labs have been fine and even assignments 1 and 2 weren't that tough. But I've been looking through my notes and I just haven't even been able to make a dent in this...

http://pastebin.com/znJzavuL

Specifically I've yet to even be able to print out the set. I've not been able to find anything on google for the loading a masking bit pattern...
 

Koren

Member
I don't believe it's ASM.

Write MIPS assembly functions to do the following.
It is, or I've missed something?

But I'm wondering which version exactly, and how you want to do I/O...

Just quickly in pseudo-code:
1. Print out a set:

I would do this:
>> put 0x01 in a register
>> put the bit string in another
A> test the smallest bit in the second register
>> if smallest bit is 1, print the first register content (as an integer)
>> right shift the second register
>> increment the first register
>> if the first register is lower/equal to 32, jump to A

Edit : it's not a masking bit pattern, actually, and you can instead put 0x01 in a register, and left-shift it at each iteration (and don't right-shift the bit string value). Instead of testing the smallest bit, you'll do a bitwise AND between the left-shifted 0x01 and the bit string and test for zero (not in set) or non-zero (in set)) You still need the counter to display the right numbers, but you can test the left-shifted register, if it's 32 bits, for zero as a termination.

2. Determine if an element is a member of a given set.
>> put 0x01 in register
>> put element in a second one
B> decrement the second
>> if the second equals 0, jump A
>> left shift first register
>> jump B
A> bitwise AND between the bitstring and the first register
>> if equals 0, print "Is a not member" else "Is a member"

3. Determine the union of two sets.
That's just a bitwise OR between both 32-bits values of each bitstreams...

4. Determine the intersection of two sets.
Same but with a bitwise AND.

I'll try to translate into actual MIPS assembly tomorrow if you have answers for my two first questions...
 

Kalentan

Member
It is, or I've missed something?

Oh. ASM = Assembly... Sorry, he usually says Assembly so when I saw that ASM I was bit thrown off. Also tired so my reading comprehention isn't the best right now.

I'll try to formulate it through that psuedo code. Thanks.
 
Really?

Which part, exactly?

It's just not very easy on the eyes. You're right that nothing was especially complex, but combined all together it's just very dense and doesn't exactly communicate its intentions. For example, there's already functions called islower() and isupper(). It's easy to write x >= 'A' && x <= 'Z' but at the same time isupper and islower exist for a reason.

The 'b' - 'A' thing is also confusing. I mean again it's not rocket science but it does take some brain cycles to figure out what it's doing.

When you're staring at code 10 hours a day and / or debugging a nasty problem, you want to spend as little time as possible thinking about what it does.

Do you prefer
Code:
for(;*str;++str) {
    if ( islower(*str) )
        (*str) = 'A' + ( ( (*str) - 'a' + 1 ) %26 );
    else if  ( isupper(*str) )
        (*str) = 'a' + ( ( (*str) - 'A' + 1 ) %26 );
}
I like the use of islower and isupper, but I still am not crazy about the conversion. It's a perfectly reasonable way to do it, don't get me wrong, just as a matter of preference I would go with something I can understand without spending any brain cycles on it.

Also I like the "if (!isalpha(*str)) continue;" because it communicates the intentions very clearly "If this isn't a letter, ignore it" otherwise you have to scan through the whole loop to figure out that this algorithm deals only with letters. Not a big deal with a 4 line function, but if it's a 40 line function, having all your assumptions up at the top makes it easier to understand IMO
 

Koren

Member
it's just very dense and doesn't exactly communicate its intentions.
I agree... but comments are nice for this.

For example, there's already functions called islower() and isupper(). It's easy to write x >= 'A' && x <= 'Z' but at the same time isupper and islower exist for a reason.
That's true, but the reasons I avoided them were:
- they are library functions, so they may not be readily available, wasn't sure in that case
- it's locale dependant, if I'm not mistaken, which can cause really strange bugs if you're not cautious ('é' for example can return True for islower, and if it's the case, you'll get a segfault when fetching the char in the const char)
- it's slower, even more so if you test isalpha then islower/isupper, and if you work in plain C, I'd argue that you may care about speed

The 'b' - 'A' thing is also confusing. I mean again it's not rocket science but it does take some brain cycles to figure out what it's doing.
I guess it does, but I may have done too many low-level programming and assembly, it's easier to read for me than a bunch of tolower() and fetchs in const strings...

I may be the strange guy there, though.

At the end of the day, you'll read faster the kind of code you'll usually write, anyway.

When you're staring at code 10 hours a day and / or debugging a nasty problem, you want to spend as little time as possible thinking about what it does.
I know... I do less actual coding now than a couple years ago (although a LOT more CS teaching, and believe me, reading code from average students is miles harder than anything fancy from actual programmers ^_^ I have a bunch of students that fancy using, in Python 3k, x//=1. to get the integer part of x... talk about a strange idea)

don't get me wrong, just as a matter of preference I would go with something I can understand without spending any brain cycles on it.
Oh, I perfectly understand your concerns... I was truly curious about what was the hardest part exactly.

Again, since I'm teaching a lot currently (to beginners, but not C... Python and CaML), so I'm interested in anything that could be natural for me and hard to understand for others. Especially since I'll do strings soon (but in both Python and CaML, there's no implicit casts between chars and ints, so you can't do anything like this, anyway).
 

Kalentan

Member

Does this seem right thus far for the first part?

Code:
li $t4, 0
li $t5, 32
li $t7, 0x1
		
	loop:
		
	lb $t6, 0 ($s3) #$s3 is Set1
			
	li $v0, 1		#System call code for Print
	move $a0, $t4	#load add.dress of prompt into $a0
	syscall	
			
	beq $t4, $t5, output
	addiu $s3, $s3, 1
	addi $t4, $t4, 1
	b loop
 
I agree... but comments are nice for this.


That's true, but the reasons I avoided them were:
- they are library functions, so they may not be readily available, wasn't sure in that case
- it's locale dependant, if I'm not mistaken, which can cause really strange bugs if you're not cautious ('é' for example can return True for islower, and if it's the case, you'll get a segfault when fetching the char in the const char)
- it's slower, even more so if you test isalpha then islower/isupper, and if you work in plain C, I'd argue that you may care about speed


I guess it does, but I may have done too many low-level programming and assembly, it's easier to read for me than a bunch of tolower() and fetchs in const strings...

I may be the strange guy there, though.

At the end of the day, you'll read faster the kind of code you'll usually write, anyway.


I know... I do less actual coding now than a couple years ago (although a LOT more CS teaching, and believe me, reading code from average students is miles harder than anything fancy from actual programmers ^_^ I have a bunch of students that fancy using, in Python 3k, x//=1. to get the integer part of x... talk about a strange idea)


Oh, I perfectly understand your concerns... I was truly curious about what was the hardest part exactly.

Again, since I'm teaching a lot currently (to beginners, but not C... Python and CaML), so I'm interested in anything that could be natural for me and hard to understand for others. Especially since I'll do strings soon (but in both Python and CaML, there's no implicit casts between chars and ints, so you can't do anything like this, anyway).

Btw, one of your original points about the lookup table, there should rarely if ever be cache misses because the whole lookup table fits in a single cache line. If you really want to be fast, you could make an array 256 bytes long with the values 0-255, except for the letters to map, and just have the entire loop be:

Code:
for (; *str != 0; ++str)
    *str = map[*str];

Compiler should even be able to vectorize this for you, and you'll still have few if any cache misses
 
Anyone care to help me with PThreads and Matrices Multiplication? Any C tutorials people can link me would be awesome. For people wondering the exact problem I'm having I'm just going to link the .pdf.

http://docdro.id/LrMs6hn

Are you having trouble with how to create and manage threads with pthreads, like trouble with just using pthreads at all? or is it more how to do matrix multiplication in parallel?
 
Top Bottom