Click here to Skip to main content
15,945,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found thatc code on the internet tha generate as many numbers as we want, increaced bu 1.

So if we want to generate 10 number it would generate 10 numbers plus one for the header.

Example.
C
10
0 1 2 3 4 5 6 7 8 9


But can I change the code to start generate the number backwards and weithout the zero value?

Example
C
10
10 9 8 7 6 5 4 3 2 1


Heres the code by Fredrik Bornander[^]:

C
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* append_char(char* buffer, char character) {
    *buffer = character;
    return ++buffer;
}
 
char* append_number(char* buffer, int number, char* temp_buffer) {
    char* target = buffer;
    char* source = temp_buffer;
    char* p;
    itoa(number, temp_buffer, 10);
    for (p = source; *p != 0; ++p) {
        *target = *p;
        ++target;
    }
    return target;
}
 
int main(int argc, char* argv[]) {
    // Max int is 4294967295, add one for a 
    // possible minus sign, add one for the space delimiter
    int number_max_characters = 10 + 1 + 1;
    int count_to_generate = 1000000; // This the number of numbers you want
    char* temp_buffer = (char*)malloc(number_max_characters);
    // Allocate space for all the numbers plus one for the "header",
    // plus space for the line-break after the header and a trailing zero
    // Note that buffer is potentially too big here, will fix that by copying 
    // only what we need into result, and freeing buffer
    char* buffer = (char*)malloc((count_to_generate + 1) * number_max_characters + 2 + 1);
    char* result;
    char* p = buffer;
    int i;
    FILE* fp;
 
    // Append header and line break
    p = append_char(append_char(append_number(p, count_to_generate, temp_buffer), '\r'), '\n');
    for (i = 0; i < count_to_generate; ++i) {
        p = append_number(p, i, temp_buffer);
        if (i < count_to_generate - 1)
            p = append_char(p, ' ');
    }
 
    *p = 0; // Null terminate

    // Take the used size of buffer, allocate that to result...
    result = (char*)malloc((int)p - (int)buffer);
    // ...and copy over to result
    strcpy(result, buffer);
    free(buffer); // We're done with this one now

    printf("%s", result);
 
    fp = fopen("test.txt", "w");
    fprintf(fp, "%s", result);
    fclose(fp);
 
    return 0;
	}
Posted
Comments
Zoltán Zörgő 29-Nov-14 7:35am    
Yes.
[no name] 29-Nov-14 7:36am    
yes I can! :)
But how?
Richard MacCutchan 29-Nov-14 8:45am    
There is a lot of redundant code in the above. For example, why do you allocate a new buffer at the end just to copy the result into it to write to the file? Copy the original. Or better still forget about these huge buffers and all the formatting of numbers and spaces, and just write each item to the file as it is generated. You would then have a program that is less than half the length of the above, and is also easy to understand.

Look at your for loop: it's what does the work.
At the moment you do this:
C++
for (i = 0; i < count_to_generate; ++i) {
and a for loop has three parts, separated by ';' characters.
C++
for (initialization ; termination check ; iterate)

"Initialization" lets you set a starting value: in your case zero.
"Termination check" lets you say when the loop will stop running: in your case when i is no longer less than your maximum value.
"Iterate" lest you specify what changes each time you go round the loop: in your case you increase i by one.

So to go down, change "Iterate": i-- will subtract one instead of adding.
That means the "Termination check" needs to be changed: i > 0 will end when you reach zero.
And then you need to change the start, so it works from your maximum:
i = count_to_generate

Which gives you:
C++
for (i = count_to_generate; i > 0; i--) {

Easy, yes?
 
Share this answer
 
Comments
[no name] 29-Nov-14 7:52am    
yes, but why the first 3 numbers aren't saperated?

10
1098 7 6 5 4 3 2 1
OriginalGriff 29-Nov-14 8:20am    
Because your "if" text inside the loop explicitly doesn't add it for the first two values. So what do you think you need to change to what?
[no name] 29-Nov-14 10:12am    
from : if (i < count_to_generate - 1)
to : if (i < count_to_generate + 1)

:)
OriginalGriff 29-Nov-14 10:24am    
:thumbsup:
This is all you need:
C++
int count_to_generate = 1000000; // This the number of numbers you want
int i;
FILE* fp;

// Append header and line break
fp = fopen("test.txt", "w");
fprintf(fp, "%d\n", count_to_generate);
for (i = 0; i < count_to_generate; ++i) {
    fprintf(fp, "%d ", i);
    printf("%d ", i);
}
fprintf(fp, "\n");
printf("\n");
fclose(fp);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900