开发者

How can we use the output of a program to make further calculations? C (Linux)

开发者 https://www.devze.com 2023-01-14 23:16 出处:网络
When I write code in C, I often get confused when the questions/problems demand the usage of the output for further calculations.

When I write code in C, I often get confused when the questions/problems demand the usage of the output for further calculations.

For example, if we have to print an array and then add only the prime numbers from it

or something which is similar. I get stuck and I don't know how to tackle such questions.

As in when we take input from the user, it's easy to just say scanf() and continue.

Eventually after reading some code from some websites I get it, but I never get it in first go. Maybe I need to know something which is really fundamental so thought you guys would be the best to seek help from, as you'll always are.

I hope I'm being clear at elaborating my difficulty.

EDIT1

I want to continue processing a result within the same program. I have mentioned an example above. I'll just highlight it.

EDIT 2

@bta- thank you. Lets say:-

Example 1: A question that asks me to output a Fibonacci Series and also add all the even numbers from the output.

Example 2: A question that asks me to output an array and then just square all the even numbers from the array (output) and print them again.

I get the answer to these q开发者_Go百科uestions when I look it up on various websites, but my question is what should I do to get my fundamentals right.


If you want to continue processing something after you have printed it out, make sure that the variable that holds the data stays in scope.

Take for instance a function like:

void sample_function (void) {
    int data;

    scanf("%d\n", &data);
    printf("The input was: %d\n", data);
}

After print out the value of data, you can't do anything else with it. You reach the end of the function and when the function returns, the data that lives inside the scope of that function is freed.

It is possible to use that data after the function has returned, but you have to change your technique a bit. One possibility is to make data a global variable instead of a local variable inside the function. You can also pass back the value of data using the return value of the function, or you can pass an int* to the function and have the function store the data using that pointer. With any of these techniques, you should be able to call a function like sample_function and keep the data in scope after the function has returned.

As long as the variable holding the data doesn't go out of scope, you shouldn't have much of a problem re-using your data over and over again. Printing/displaying the data doesn't alter or destroy it, the only way that you can lose the ability to re-use the data is if the variable holding the data gets destroyed (by going out of scope or by being being freed).

From your description, it's hard to get a good picture of what exactly you are talking about. Can you give us an example (in code) of a situation like you are talking about?

Update: Based on your edits, it looks like you are not completely understanding what is happening. When you print data to the screen, you are not altering the data at all. You still have every ability to modify, access, and re-use the data just as you did before you displayed it. You shouldn't have to do anything in particular to use the data again. Using one of your examples, let's say you have an array that contains a series of numbers in a Fibonacci sequence:

int sequence[100];
int i, sum;

fill_with_fibonacci_sequence(sequence); // or however else you get your initial data

// Print out the sequence
printf("The Fibonacci sequence is: ");
for (i=0; i<100; ++i) {
    if ((i & 0x7) == 0)
        printf("\n   ");
    printf("%i ", sequence[i]);
}

// Sum all of the even numbers from the sequence
sum = 0;
for (i=0; i<100; ++i) {
    if ((sequence[i] & 0x1) == 0)
        sum += sequence[i];
}
printf("The sum of the even numbers is: %i\n", sum);

Here, we print each value in the array, then turn around and re-process the array in order to sum the even terms. Printing out the array doesn't change the array itself, so nothing special needs to be done in order to re-use the data.


I think you're misreading the problem.

If a problem says "create an array, print it and add its even numbers" doesn't means that the program must re-use its output as new input (!).

It just means:

int
main (int argc, char *argv[])
{
    int i,
        sum,
        array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10};

    for (i = 0; i < sizeof(array)/sizeof(array[0]); i++) {
        printf("%d ", array[i]);
    }
    printf("\n");

    sum = 0;
    for (i = 0; i < sizeof(array)/sizeof(array[0]); i++) {
        if (array[i] % 2 == 0) {
            sum = sum +array[i];
        }
    }
    printf("%d\n", sum);

    return EXIT_SUCCESS;
}

When you printf something, you're sending it to the kernel using a syscall. Taking it again as input just doesn't make sense.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号