• 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

Oh this solved it!! I was building it in debug and now in release it works! Thanks so much. And about the bool, I don't understand how I can return true or false when there is also a chance of returning the recursive method which isn't a boolean.

If you make prime return a bool, then the recursive call (which is a call to prime) will return a bool.
Code:
bool prime(int x, int i)
{
  if (i == 1)
    return true;
  else
  {
    if (x % i == 0)
      return false;
    else
      return prime(x, i - 1);
  }
}

In the final return statement, where it returns the recursive call, it is returning a bool (see line 1, where the function is declared as "bool prime..."
 

Makai

Member
I'm learning C++ and I'm so disappointed.

Code:
int a = 1;

wasn't good enough for C++

so they added two alternatives
Code:
int a(1);
int a{1};

I'll probably find out why later but >:||
 

vypek

Member
I'm learning C++ and I'm so disappointed.

Code:
int a = 1;

wasn't good enough for C++

so they added two alternatives
Code:
int a(1);
int a{1};

I'll probably find out why later but >:||

Oh wow. Had no idea about this. Last time I did something in C++ must have been years ago as a side project while I was in school.
 
I'm learning C++ and I'm so disappointed.

Code:
int a = 1;

wasn't good enough for C++

so they added two alternatives
Code:
int a(1);
int a{1};

I'll probably find out why later but >:||
C like assignment is for compatibility but also because it's the most understandable. Parentheses is for calling constructors. Brace initializing is used for two things: either it's for list initialization (like initializing a vector) or aggregate initialization. The latter is best explained like this: just write out all the values you want in the datatype. For instance if you have a struct S that has an int, a float, and an int, you'd write

Code:
S s{1, 1.0, 1};
 
I'm learning C++ and I'm so disappointed.

Code:
int a = 1;

wasn't good enough for C++

so they added two alternatives
Code:
int a(1);
int a{1};

I'll probably find out why later but >:||

Using the = syntax is considered an implicit conversion. So, for example, it won't work when you want to call a constructor that has been marked as explicit, or for that matter when you want to call a constructor that takes more than one parameter. Because of that, a second syntax was needed that would not invoke an implicit conversion.

To address this, the () syntax was introduced a long time ago as a way to force a constructor call. But this has its own set of problems. For starters, initializer lists were introduced so that you could invoke a constructor like std::vector<int> X = {1, 2, 3, 4, 5}; The = {} syntax is important because something had to distinguish between an implicit conversion and a constructor overload which takes an initializer list. So the 3rd syntax was needed here. But the parentheses have another set of problems too. In particular, the C++ "most vexing parse" (google it). It's lame and stupid, but it's a real problem.

But now, if you're already got this 3rd syntax, might as well make this be the one "universal" syntax that can work for everything. So that's what they did. {} is called "uniform initializer" for this very reason. You can write

Code:
int X{1};   // Initialize an integer to 1
Foo X{1}; // Call the constructor that takes an int, without requiring an implicit conversion.
vector<int> X = {1, 2, 3, 4, 5}; // Call the initializer list constructor

And there is 1 syntax. Of course there are some more nuances I didn't discuss (when are there ever NOT more nuances when it comes to C++), but that's the reason.
 
I don't know Ruby, but I think if you decrement the random value by 1 and move the decimal left, you'd have the array index, saving you the whole if/elseif block.
 

I don't know Ruby, but does it support integer division with no remainders? For example, 9 divided by 10 is 0, 15 / 10 is 1, 28 / 10 is 2, etc. If you have 10 buckets, you could then get the bucket index just by dividing by 10. No long if/elseif chains, just divide and get your index. So something like

Code:
counts[range / 10] += 1

Another tip for when this is not applicable, but just something to clean up your code. You don't need to do

Code:
if range <= 10
elseif range > 10 && range <= 20
else if range > 20 && range <= 30

The first part of each elseif is redundant (they will be caught in the prior conditional, as long as you write them in the correct order!). Simplify that.

Code:
if range <= 10
elseif range <= 20
elseif range <= 30
 
I don't know Ruby, but does it support integer division with no remainders? For example, 9 divided by 10 is 0, 15 / 10 is 1, 28 / 10 is 2, etc. If you have 10 buckets, you could then get the bucket index just by dividing by 10. No long if/elseif chains, just divide and get your index. So something like

Code:
counts[range / 10] += 1

Another tip for when this is not applicable, but just something to clean up your code. You don't need to do

Code:
if range <= 10
elseif range > 10 && range <= 20
else if range > 20 && range <= 30

The first part of each elseif is redundant (they will be caught in the prior conditional, as long as you write them in the correct order!). Simplify that.

Code:
if range <= 10
elseif range <= 20
elseif range <= 30

Of course. Yea that was extremely superfluous.

Ah interesting points.
Yea I could see that working and reducing the work considerably. I'll have to check later if it forces through Dec/floats. But regardless I think I could force it to do int math.
 

Koren

Member
Yea I could see that working and reducing the work considerably. I'll have to check later if it forces through Dec/floats. But regardless I think I could force it to do int math.
It stays with int...

This means you can write
Code:
ranges.each { |x| set[(x-1)/10] += 1 }
where set is a array of 10 ints

But I'd even do
set = Array.new(10){""}
ranges.each { |x| set[(x-1)/10] += '*' }
puts set.join("\n")

I should do more Ruby... Does somebody know whether you can merge the previous lines in a single one?
 
How the hell do you do all that formatting in a few lines of code though. I figured there would be a way to index easily like that (turns out it was decievingly simple). But I'm at a huge loss there as well.

I'm still freaking out that he said he's seen it done in 2 lines. Apparently he's going to show us the code next week.


It stays with int...

This means you can write
Code:
ranges.each { |x| set[(x-1)/10] += 1 }
where set is a array of 10 ints

But I'd even do
set = Array.new(10){""}
ranges.each { |x| set[(x-1)/10] += '*' }
puts set.join("n")

I should do more Ruby... Does somebody know whether you can merge the previous lines in a single one?

I don't think we've covered/looked at the set class yet. But I did use it in a Switch case. So I like it lol.
 

Koren

Member
How the hell do you do all that formatting in a few lines of code though. I figured there would be a way to index easily like that (turns out it was decievingly simple). But I'm at a huge loss there as well.

I'm still freaking out that he said he's seen it done in 2 lines. Apparently he's going to show us the code next week.
Well, you have a solution in 3 lines above ;) And I haven't done Ruby in ages, I'm sure there's better.
 
You can repeat a character N times with the * operator. So each row is just

Code:
str(10K-9) + "-" + str(10K) + "   | " + str(N[K]) + " |    " + "*" * N[K]

where you iterate K from 1 to 10 (pseudocode since I don't know Ruby, you get the idea though)
 

Koren

Member
I don't think we've covered/looked at the set class yet. But I did use it in a Switch case. So I like it lol.
It's not a set, it's an array of integers...

Code:
set = Array.new(10, 0)

set isn't a great choice, but since you called your counters set_a, set_b, etc., I kept the name.
 
It's not a set, it's an array of integers...

Code:
set = Array.new(10, 0)

set isn't a great choice, but since you called your counters set_a, set_b, etc., I kept the name.

I'm going to go to sleep. And take a look later this afternoon so I hopefully stop embarrassing myself. Thanks for the assist.
 
Well, you have a solution in 3 lines above ;) And I haven't done Ruby in ages, I'm sure there's better.

I think I need to clarify, that I need to print out everything there, not just what's under the chart header. I need to print all those headers, the ranges, times found etc.

In the code

ranges.each { |x| set[(x-1)/10] += '*' }

I'm assuming I can just substitute out per line here with formatting, but I'd again have to do a bunch of if, elsif statements as I don't know a better way. Which would bring me way over. I think I'll just write it up in such a way as it's the limit of my abilities for now. But I'd like to know how you could approach that.
 
I think I need to clarify, that I need to print out everything there, not just what's under the chart header. I need to print all those headers, the ranges, times found etc.

Does my suggestion not work? Again I don't know ruby but it seems fairly straightforward given the repetition operator
 
Does my suggestion not work? Again I don't know ruby but it seems fairly straightforward given the repetition operator

str(10K-9) + "-" + str(10K) + " | " + str(N[K]) + " | " + "*" * N[K]

I'm going to say I honestly don't understand what you're doing here.

are you saying output whatever string I want within ( ) parameters? Where K is incrementing from 10 - 1 back up to 10? What is N representing here?
 
str(10K-9) + "-" + str(10K) + " | " + str(N[K]) + " | " + "*" * N[K]

I'm going to say I honestly don't understand what you're doing here.

are you saying output whatever string I want within ( ) parameters? Where K is incrementing from 10 - 1 back up to 10? What is N representing here?

So you need to output 10 rows in the table. So K simply iterates from 1 to 10. Each row looks like this:

Code:
 10K-9  -  10K      |   Histogram[K-1]   |   "*" repeated Histogram[K-1] times.

Histogram is your array. In your example picture Histogram is {28, 18, 21, 26, 23, 7, 18, 24, 14, 22}. So, for example, Histogram[7] is the item at index 7 item, which is 24 (assuming Ruby is 0-based, which I don't know).

So if you iterate K from 1 to 10, then a single row of output can be written by printing the value of 10K-9, printing " - ", then printing the value of 10K, then printing " | ", then printing the value of the K-1'th histogram value, then printing " | ", then printing the character "*" repeated Histogram[K-1] times, which can be done by writing "*" * Histogram[K]. (At least according to a quick google search which says that this is how you use the repetition operator).

So that's one line to print a single row. You should be able to extend this to print all rows in a single line by making this some kind of lambda (which again I don't know the syntax for in Ruby) and using some kind of functional method like apply or foreach.

For example, in Python I can do it with this line:

Code:
histogram = [28, 18, 21, 26, 23, 7, 18, 24, 14, 22]

print "\n".join(map(lambda K: "{:2} - {:3}    | {:2} |    {}".format(10*K-9, 10*K, histogram[K-1], "*" * histogram[K-1]), range(1, 11)))

which prints this:

Code:
 1 -  10    | 28 |    ****************************
11 -  20    | 18 |    ******************
21 -  30    | 21 |    *********************
31 -  40    | 26 |    **************************
41 -  50    | 23 |    ***********************
51 -  60    |  7 |    *******
61 -  70    | 18 |    ******************
71 -  80    | 24 |    ************************
81 -  90    | 14 |    **************
91 - 100    | 22 |    **********************
 
So you need to output 10 rows in the table. So K simply iterates from 1 to 10. Each row looks like this:

Code:
 10K-9  -  10K      |   Histogram[K-1]   |   "*" repeated Histogram[K-1] times.

Histogram is your array. In your example picture Histogram is {28, 18, 21, 26, 23, 7, 18, 24, 14, 22}. So, for example, Histogram[7] is the item at index 7 item, which is 24 (assuming Ruby is 0-based, which I don't know).

So if you iterate K from 1 to 10, then a single row of output can be written by printing the value of 10K-9, printing " - ", then printing the value of 10K, then printing " | ", then printing the value of the K-1'th histogram value, then printing " | ", then printing the character "*" repeated Histogram[K-1] times, which can be done by writing "*" * Histogram[K]. (At least according to a quick google search which says that this is how you use the repetition operator).

So that's one line to print a single row. You should be able to extend this to print all rows in a single line by making this some kind of lambda (which again I don't know the syntax for in Ruby) and using some kind of functional method like apply or foreach.

Ah ok I think I see how you're laying that out, but I'm not sure how to properly access

So for instance then, I would need to reach this point and fill for those values is something like

Array = stores 200 random numbers between 1-100
second array = stores 1-10 index, each is defined as an empty string.
for each of the values in Array takes the value, divides it by ten then counts? (I assume I need a counter somewhere?) the number of times each number from 1-10 occurs and stores it in the corresponding index.

I'm sure the answer is here

counts[(range-1) / 10] += 1

but I don't understand how it works in practice.



Then I'm simply substituting in for what you've outlined essentially per row and extend it.

The problem is now my... lack of understanding about arrays I guess. How do I call the index value instead of the stored value in the array for the first part in ruby? That's what I'm trying to do right?

So 10*index - 10*index | second_array[value-1} | "*" repeated * second_array[value-1] times.

How in the second array I have, do I have 1..10, then iterate by 1 for each occurrence of each number without doing a long if/elsif with counters?
 
Ah ok I think I see how you're laying that out, but I'm not sure how to properly access

So for instance then, I would need to reach this point and fill for those values is something like

Array = stores 200 random numbers between 1-100
second array = stores 1-10 index, each is defined as an empty string.
for each of the values in Array takes the value, divides it by ten then counts? (I assume I need a counter somewhere?) the number of times each number from 1-10 occurs and stores it in the corresponding index.

I'm sure the answer is here

counts[(range-1) / 10] += 1

but I don't understand how it works in practice.



Then I'm simply substituting in for what you've outlined essentially per row and extend it.

The problem is now my... lack of understanding about arrays I guess. How do I call the index value instead of the stored value in the array for the first part in ruby? That's what I'm trying to do right?

So 10*index - 10*index | second_array[value-1} | "*" repeated * second_array[value-1] times.

How in the second array I have, do I have 1..10, then iterate by 1 for each occurrence of each number without doing a long if/elsif with counters?

This link suggests you can write something like *(1..10) to get an array from 1 to 10.

To get an array of 200 random numbers this link suggests you could maybe do something like *(1..200).collect{|x| rand(1,100)}

If that does what I think it does, then the *(1..200) is generating the array [1,2,3,...,200], and the .collect is calling the function in {} for every value in the array. Since there are 200 items in the array, it's calling it 200 times. Each time creates a random number, and the result is a new array with all these values.

So now you've got your array from 1 to 10, and your array of 200 random integers. What you need to do is iterate over each of those integers, and print them. It will be similar in spirit to my statement in Python where you use a lambda and map to return a list of each row formatted as strings, followed by something to join it all and print it.

Honestly you could probably get the entire program down to 1 line this way.
 
This link suggests you can write something like *(1..10) to get an array from 1 to 10.

To get an array of 200 random numbers this link suggests you could maybe do something like *(1..200).collect{|x| rand(1,100)}

If that does what I think it does, then the *(1..200) is generating the array [1,2,3,...,200], and the .collect is calling the function in {} for every value in the array. Since there are 200 items in the array, it's calling it 200 times. Each time creates a random number, and the result is a new array with all these values.

Oh I'm not having any issue creating the array with 200 random numbers from 1 to 100. What I'm trying to figure out is how to access the index value independently from the stored value. And how to store in a new array the values of each of them /10 without using a bunch of if/elsif statements.

I've never used .collect before, we haven't really covered it. I have a feeling I should just stick to what I know and then bring this up with him separately to suss out the rest. It's just driving me crazy.

edit:

I don't think he would approve of me using .collect, .join or things like that. He kind of likes us to use the toolsets he's shown us. I've already got chided for taking "shortcuts" with the more powerful functionality. Which I do get. I want to understand why the code I'm using does what it does. I need to understand it.
 
Oh I'm not having any issue creating the array with 200 random numbers from 1 to 100. What I'm trying to figure out is how to access the index value independently from the stored value. And how to store in a new array the values of each of them /10 without using a bunch of if/elsif statements.

I've never used .collect before, we haven't really covered it. I have a feeling I should just stick to what I know and then bring this up with him separately to suss out the rest. It's just driving me crazy.

Yea I don't think you should worry about getting it down to 1-2 lines. It almost certainly uses stuff you haven't learned yet, and he was just telling you this stuff as a way to pique your curiosity, even if you don't have the tools to do it.

Usually this type of thing involves functional programming. Where loops and conditionals are replaced by functions that internally operate on every item of a collection.

In my Python example, to access the index and the stored value, I have a list of all integers from 1 - 10. So basically the indices are in a list. And I use a functional algorithm to iterate over the indices and call a function for every index. Then inside that function I can use the argument (i.e. the index) to get the corresponding value from the histogram.
 

Ahnez

Member
Oh I'm not having any issue creating the array with 200 random numbers from 1 to 100. What I'm trying to figure out is how to access the index value independently from the stored value. And how to store in a new array the values of each of them /10 without using a bunch of if/elsif statements.

I've never used .collect before, we haven't really covered it. I have a feeling I should just stick to what I know and then bring this up with him separately to suss out the rest. It's just driving me crazy.

Yep, specially on introdutory classes, don't use what you don't understand. EVER.

You can do like in the code you first posted

Code:
ranges.each do |range|
  histogram[(range-1)/10] += 1

Each entry on histogram[] is a counter of some sort. For numbers between 1 and 10, the index will become 0, between 11 and 20 will be 1, etc

So, for every number between 1 and 10, you are adding 1 to histogram[0], each number between 11 and 20, you're adding 1 to histogram[1] and so on.

In the end, histogram will store how many numbers between 1+10i and 10(i+1) you have
 
Yea I don't think you should worry about getting it down to 1-2 lines. It almost certainly uses stuff you haven't learned yet, and he was just telling you this stuff as a way to pique your curiosity, even if you don't have the tools to do it.

Usually this type of thing involves functional programming. Where loops and conditionals are replaced by functions that internally operate on every item of a collection.

In my Python example, to access the index and the stored value, I have a list of all integers from 1 - 10. So basically the indices are in a list. And I use a functional algorithm to iterate over the indices and call a function for every index. Then inside that function I can use the argument (i.e. the index) to get the corresponding value from the histogram.

I appreciate all your help. I did learn a few things, which is all I can ask for. Maybe down the line it will all click into place.

As for my current code, basically it looks like this, and it works as intended. The issue is I don't understand how the iterator here is working. I think that would be a big step for me to understand here.

rand_nums= Array.new(200) { rand(1..100)}
set = Array.new(10){0}
rand_nums.each {|range| set[(range-1) / 10] += 1}

so I've created an array with 10 values, starting from index 0, and defined the values as a 0.

so for each of the numbers in the rand_numbers array I'm taking it evaluating the result-1 to compensate and then dividing by 10. I want to know how the iterator works here though. How is this accounting for each value independently.

Edit:

Yep, specially on introdutory classes, don't use what you don't understand. EVER.

You can do like in the code you first posted

Code:
ranges.each do |range|
  histogram[(range-1)/10] += 1

Each entry on histogram[] is a counter of some sort. For numbers between 1 and 10, the index will become 0, between 11 and 20 will be 1, etc

So, for every number between 1 and 10, you are adding 1 to histogram[0], each number between 11 and 20, you're adding 1 to histogram[1] and so on.

In the end, histogram will store how many numbers between 1+10i and 10(i+1) you have


yea it does me no good to use code I don't understand. Ok I see how the math is working here, but how is the program knowing to change which index is being iterated, that's what I don't understand.
 
I appreciate all your help. I did learn a few things, which is all I can ask for. Maybe down the line it will all click into place.

As for my current code, basically it looks like this, and it works as intended. The issue is I don't understand how the iterator here is working. I think that would be a big step for me to understand here.

rand_nums= Array.new(200) { rand(1..100)}
set = Array.new(10){0}
rand_nums.each {|range| set[(range-1) / 10] += 1}

so I've created an array with 10 values, starting from index 0, and defined the values as a 0.

so for each of the numbers in the rand_numbers array I'm taking it evaluating the result-1 to compensate and then dividing by 10. I want to know how the iterator works here though. How is this accounting for each value independently.

This looks like a lambda / functional paradigm. The stuff after "rand_nums.each" is actually a miniature function, and it's calling that function for every item in the collection. All the iteration is happening inside of the "each" function (you can't see the code for it). And on each iteration, it's executing the code inside those curly braces, where the value of "range" is equal to the item from the current iteration.
 

Ahnez

Member
so for each of the numbers in the rand_numbers array I'm taking it evaluating the result-1 to compensate and then dividing by 10. I want to know how the iterator works here though. How is this accounting for each value independently.

I'm not sure if I understood, but, in Ruby, the each iterator takes objects from something (numbers from the random array, in this case), stores them in a variable ( in the variable between '|', range in this case ), and for every object it iterates, it executes a block of code ( set[(range-1) / 10] += 1 , in this case)
 
This looks like a lambda / functional paradigm. The stuff after "rand_nums.each" is actually a miniature function, and it's calling that function for every item in the collection. All the iteration is happening inside of the "each" function (you can't see the code for it). And on each iteration, it's executing the code inside those curly braces, where the value of "range" is equal to the item from the current iteration.

I'm not sure if I understood, but, in Ruby, the each iterator takes objects from something (numbers from the random array, in this case), stores them in a variable ( in the variable between '|', range in this case ), and for every object it iterates, it executes a block of code ( set[(range-1) / 10] += 1 , in this case)

OK that makes sense. Thanks!
 
Edit: nevermind I don't need one. I was so dense. Damn. Like you said I can just iterate from 1-10, no need for indexes at least here. Would be helpful to know for the future though.
 

Mine01

Member
So gaf i have a bit off a problem, idk what I should do hope u guys can help me.

So I have 3 Systems that share information.

1- Administration (billing stuff like that) System
2-Going from raw material to finished product System.
3-Accounting system.

Now, I need info to go from
2 to 1
2 to 3
1 to 3

I know I kinda hardcode that stuff but I wanna make something that works even when the client isnt using one of our products.

Something like x to 3 or stuff like that.

1 is in aspx, 2 and 3 mvc with full service layer and shit like that.

I was thinking I could make a little Interface system(4) so it Would be like that.

2 to 4 to 1
2 to 4 to 3
1 to 4 to 3
x to 4 to 3

My idea is having a webservices catalogue in 4 where you cand add webservices links and the system could see what is on the other end, and then you can "transform" (somehow) that information in 4 before sending it to the other end.

I'veen looking at stuff like automapper and xlt but I dont really know if there is an easy or better way to do this.

I don't know if I'm explaining myself well, hope I do and anyone can help me here! Thx!
 

Kelsdesu

Member
Alright. Basically I have two merged text files already. My problem is I can't get it formatted the way I want.

last name and first name in the merged text file but seperated with a tab. I'm thinking a for loop with the use of "t". Am I on the right track? Any place where there is some reading I can look at involving File I/O manipulation?

Thanks yall.

In C.
 

Mine01

Member
Alright. Basically I have two merged text files already. My problem is I can't get it formatted the way I want.

last name and first name in the merged text file but seperated with a tab. I'm thinking a for loop with the use of "t". Am I on the right track? Any place where there is some reading I can look at?

Thanks yall.

In C.

Don't you use an "t"??

What is the loop for?

Normal escape sequences (n,t) should work even in file manipulation,afaik
 

cyborg009

Banned
Any good recommendations for good powershell scripting tutorials? I haven't used it in ages but these repetitive task at work are such a waste of time.
 

Kelsdesu

Member
Don't you use an "t"??

What is the loop for?

Normal escape sequences (n,t) should work even in file manipulation,afaik


I was thinking the loop would go through the list of last names in the merged file and tab it over. Maybe Im thinking too hard about it.
 

Mine01

Member
I was thinking the loop would go through the list of last names in the merged file and tab it over. Maybe Im thinking too hard about it.
You could add a "t" after the first name and then concatenate both first name and last name.

How do you generate this merged file content?
 

Koren

Member
Aztechnology: Sorry for not being here to help earlier, but it seems you found the help you needed...

I was thinking the loop would go through the list of last names in the merged file and tab it over. Maybe Im thinking too hard about it.
You have a file with one name per line, and one file with one first name per line?

What about something like
Code:
char* buf1 = NULL;
int sizbuf1 = 0

char* buf2 = NULL;
int sizbuf2 = 0

while ( (getline(&buf1, &sizbuf1, file1) != -1)
            && (getline(&buf2, &sizbuf2, file2) != -1) ) {
    
    # Remove CR/LF from first buffer
    buf1[strcspn(buf1, "\n\r")] = 0;

    # Print result
    fprintf( fileout, "%s \t%s", buf1, buf2 );
}

# Free allocated buffers
if (buf1) { free(buf1); }
if (buf2) { free(buf2); }
 

Mine01

Member
So gaf i have a bit off a problem, idk what I should do hope u guys can help me.

So I have 3 Systems that share information.

1- Administration (billing stuff like that) System
2-Going from raw material to finished product System.
3-Accounting system.

Now, I need info to go from
2 to 1
2 to 3
1 to 3

I know I kinda hardcode that stuff but I wanna make something that works even when the client isnt using one of our products.

Something like x to 3 or stuff like that.

1 is in aspx, 2 and 3 mvc with full service layer and shit like that.

I was thinking I could make a little Interface system(4) so it Would be like that.

2 to 4 to 1
2 to 4 to 3
1 to 4 to 3
x to 4 to 3

My idea is having a webservices catalogue in 4 where you cand add webservices links and the system could see what is on the other end, and then you can "transform" (somehow) that information in 4 before sending it to the other end.

I'veen looking at stuff like automapper and xlt but I dont really know if there is an easy or better way to do this.

I don't know if I'm explaining myself well, hope I do and anyone can help me here! Thx!

Today i'm starting to change some mvc controllers to api controllers so I can start writting this (4) system, so if my idea is stupid is the moment to tell me gaf!
 

Kelsdesu

Member
You could add a "t" after the first name and then concatenate both first name and last name.

How do you generate this merged file content?

the content is already there in text form. Its just tricky when you have 100 names.

Aztechnology: Sorry for not being here to help earlier, but it seems you found the help you needed...


You have a file with one name per line, and one file with one first name per line?

What about something like
Code:
char* buf1 = NULL;
int sizbuf1 = 0

char* buf2 = NULL;
int sizbuf2 = 0

while ( (getline(&buf1, &sizbuf1, file1) != -1)
            && (getline(&buf2, &sizbuf2, file2) != -1) ) {
    
    # Remove CR/LF from first buffer
    buf1[strcspn(buf1, "\n\r")] = 0;

    # Print result
    fprintf( fileout, "%s \t%s", buf1, buf2 );
}

# Free allocated buffers
if (buf1) { free(buf1); }
if (buf2) { free(buf2); }


hm I see what you did. I wasn't using "fprintf", but "fputc". Something so simple.

That was really helpful.
 

Somnid

Member
Today i'm starting to change some mvc controllers to api controllers so I can start writting this (4) system, so if my idea is stupid is the moment to tell me gaf!

Seems like a sensible idea. Each part of the architecture should have a generic, well defined, stateless, interface and each consumer app might have a lightweight webserver that deals with transforming, aggregating, caching data into view models for the app. Try to keep app-specific logic separate from business logic.
 

Mine01

Member
Seems like a sensible idea. Each part of the architecture should have a generic, well defined, stateless, interface and each consumer app might have a lightweight webserver that deals with transforming, aggregating, caching data into view models for the app. Try to keep app-specific logic separate from business logic.

I havent tought about using things like WCF so im looking into that now.

Adding a "wcf layer" to each project, that would recive the info from the other systems and then send it to its service layer so i can validate before doing crud stuff.

Idk wish one seems like a better option, but I think I'll go for WCF.
 

Somnid

Member
I havent tought about using things like WCF so im looking into that now.

Adding a "wcf layer" to each project, that would recive the info from the other systems and then send it to its service layer so i can validate before doing crud stuff.

Idk wish one seems like a better option, but I think I'll go for WCF.

WCF is kinda terrible. Make a REST service using WebAPI or better yet ASP.net Core.
 

Mine01

Member
WCF is kinda terrible. Make a REST service using WebAPI or better yet ASP.net Core.

Okk thx gonna look into ASP.net core then.

What should I look for at ASP.net Core?? REST Services??

A little more info about this projects.

Both MVC projects use Kendo Framework, so all of my controllers are REST ready (I think?)

What im looking for is a way to share data, and transform that data between them.
 

Somnid

Member
Okk thx gonna look into ASP.net core then.

What should I look for at ASP.net Core?? REST Services??

Yeah. You'll be building HTTP endpoints. Asp.Net Core is just the newer way of doing as it merges WebApi and MVC and can be run cross-platform.
 
Because I'm a loser with nothing better to do, here is the whole program in 2 lines of Python.

Code:
histogram = [len(list(filter(lambda x : x > y and x <= y+10, list(map(lambda z: random.randint(1, 100), range(0, 200)))))) for y in range(0,100,10)]
print("\n".join(["{:<9} {:<8}{:<8}".format("Range", "Result", "Graph"), "-"*9 + " " + "-"*7 + " " + "-"*8] + list(map(lambda K: "{:2} - {:3}  | {:2} |  {}".format(10*K-9, 10*K, histogram[K-1], "*" * histogram[K-1]), range(1, 11)))))

Not sure if there's a way to get this down to 1 line because histogram consists of random numbers so all accesses need to refer to the same reference, you can't just regenerate it every time. Maybe someone can figure it out though.

Probably involves putting all this stuff inside of another lambda and passing histogram into the lambda.
 

Koren

Member
Not sure if there's a way to get this down to 1 line because histogram consists of random numbers so all accesses need to refer to the same reference, you can't just regenerate it every time.
Yes, there is... List are handled by references, so [L]*10 means a list containing ten times the SAME list L. It doesn't even involve copying (it's like a list of pointers), so there's not even efficiency issues.

You don't need lambda either... Just list comprehensions, generators and iterators.

Code:
print("Range     Result  Graph\n--------- ------- --------\n" + "\n".join("{:2} - {:3}  | {:2} |  {}".format(10*k+1, 10*(k+1), s, '*'*s) for k, s in ( (k, sum(1 for e in l if 10*k<e<=10*k+10)) for k, l in enumerate([[randint(1, 100) for _ in range(200)]]*10) )) )

Granted, that's not really readable, but it's a one-liner.

It can probably be shorten, btw...

(Edit : it's easier with Counters from collections and/or numpy, but I though it was cheating ;) I used randint, though, even if it could be done without importing anything, you can do a PRNG generator in a one-liner, even if it's really, really... dirty)
 

Koren

Member
The randint-less (and thus import-free) version, using Park and Miller version of Lehmer PRNG, a dirty trick with id() to get a pseudo-seed (since I can't use time or anything like this), and a dirtier trick to create a generator which isn't stateless:

Code:
print("Range     Result  Graph\n--------- ------- --------\n" + "\n".join("{:2} - {:3}  | {:2} |  {}".format(10*k+1, 10*(k+1), s, '*'*s) for k, s in ( (k, sum(1 for e in l if 10*k<e<=10*k+10)) for k, l in enumerate([[ (L.append(L.pop() * 16807 % 2147483647), L.append(L[-1]), L.pop()%99+1)[2] for L in ([[id("This is a trick to get a seed")]]*200) ]]*10) )) )

A bit shorter:
Code:
print("\n".join(["Range     Result  Graph","-"*9+" "+"-"*7+" "+"-"*8]+["{:2} -{:4}  |{:3} |  {}".format(k,k+9,s,'*'*s)for k,s in((k,sum(k<=e<k+10 for e in l))for k,l in zip(range(1,92,10),[[(L.extend([L.pop()*16807%2147483647]*2),L.pop()%99+1)[1]for L in([[id(dict())]]*200)]]*10))]))
 
Top Bottom