[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Programming Efficiency

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 12
Thread images: 1

File: prog-languages[1].png (135KB, 620x258px) Image search: [Google]
prog-languages[1].png
135KB, 620x258px
First year CSE student. I don't know jackshit. I see all those posts kek'ing about how easy Bubblesort is and I'm like "What's Bubblesort?". Anyways, how do you guys learn efficiency? Here I am doing in 20 lines what should only take 2 lines. I need to know.

>inb4 don't do comp sci fag
>>
Efficiency isn't relevant to lines per say (mostly readability and cleanliness), but more so with performance complexity and space efficiency. As in, the more elements, n, you give your program, how does it perform?

Honestly don't worry about minimizing your number of lines for now.
>>
>>59623337
I'm not trying to minimize my lines per se. But my programs are just ridiculously inefficient. I don't even realize how bad they are until I look at someones else and wonder to myself why I didn't just do that. My programs do work and meet requirements in homeworks and quizzes and whatnot, but it's just a generally larger file size vs other students.
>>
>>59623535

Give us an example OP.
>>
>>59622987
you learn efficiency by being inefficient, lazy (fuck man I don't want to spend 20 lines on this shit, sure it could be done faster) and willing to improve

just program, it's a skill like any other. I'm sure I wrote way more horrible shit when I started @ uni than I do now.
practice, practice, practice, ..., practice
>>
>>59623658
void read_list(int a[], const int max, int &size)
{
cout << "Enter non-negative numbers (ints) terminated by -99" << endl;
for(int i=0;i<=max;i++)
{
size=i;
cin >> a[i];
if(a[i]==-99)
{
break;
}
}
cout << endl;
}
void print_array(int a[], int size)
{
cout << "Original list (" << size << " values):" << endl;
for(int i=0;i<=size-1;i++)
{
if(a[i]<0)
{

}
else
{
cout << a[i];

if(i<size-1)
{
cout << ", ";
}
else
{
cout << ".";
}
}
}
cout << endl;
cout << endl;
}
int find_max(int a[], int size)
{
int pos(0); //position in Array

for(int i=0;i<=size-1;i++)
{
if(a[pos]<a[i])
{
pos = i;
}
}
return pos;
}
void array_add(int a[], int large, int size)
{
cout << "Modified list (" << size << " values):" << endl;
for(int i=0;i<=size-1;i++)
{
if(a[i]<0)
{

}
else
{
a[i] += large;
cout << a[i];

if(i<size-1)
{
cout << ", ";
}
else
{
cout << ".";
}
}
}
cout << endl;
}


A homework I did. My friends was more efficient.
>>
>>59623701

efficiency is not about numbers of lines, it's about quality of algorithm
a rookie programmer's mistake is to condense everything into as few lines of code as possible, rendering it unreadable
>>
>>59622987
Efficiency is like cumming shitting and pissing at the same time.
If you think this is good idea then I suggest reading multiple books at a time.
>>
>>59623811
Butthurt webdev codemonkey detected.
>>
>>59623767

I'm guessing this was the task?
>Input a list of integers
>Print it out again
>Find the largest element in the array
>Add that element to everything in the list, then print the modified list.

Algorithmically it looks fine. There's not much complicated going on here in terms of the mathematical operations so there's not much to get wrong - and in the end, once it's compiled, your program will be nearly identical to any other program that implements this functionality.

If you were asked to describe a way to sort a list of integers in plain English, you could think of many different methods to get there. You can't come up with many different ways of finding the biggest number in an unordered list, or adding a number to everything in a list.

Algorithmic complexity is very different to readability though, and readability is the main issue with your code.

Look at this bit for example.
if(a[i]<0)
{

}
else
{
...
}

This kind of thing would throw off most people reading your code. You'd better rewrite it the other way around and have a comment somewhere to say why.
// Print only positive integers bc negatives terminate
if (a[i] >= 0)
{
...
}

Alternatively, you could just pass the print_array and add_array functions a smaller size parameter for only the number of positive numbers in the list, so you could remove that "magic" condition.

There's also the add_array function. There's a lot of repeated code in there for printing out the value... Why not just call the print_array function instead? Let the add_ array function worry about adding, and let the print_array function worry about printing.

As far as the add_array function is concerned, why does it matter if the increment is "large"? Call it what it does.

These are just some guidelines when it comes to organising and structuring your code. Try to make it as easy as possible for the reader to know what you intend.
>>
>>59624351
You are correct as to what it's supposed to do. The homework required those 4 functions. Literally said you must have read_list, print_array, find_max, and array_add to get full points.

The code you pulled is a good example. My friend simply tossed any negative value in the read_list function itself so he didn't even need
if(a[i]>=0)


Thanks for your suggestions.
>>
>>59622987
efficiency is something you are born with
Thread posts: 12
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.