Szabolcs
Szabolcs

Reputation: 11

How to use block in c

I have one block with integers, I need to do some task in school.

I need to group even numbers to one block (even numbers) and i need to group odd numbers to one block (odd numbers)

I was trying to do something, but this doesn't work normally

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

int main()
{
    int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
    int parostomb[] = {};
    int paratlantomb[] = {};
    int n = 9;
    int i = 0;
    int j = 0;
    int paros = 0;
    int paratlan = 0;
    
    for (i=0; i < n; i++){
        if (tomb[i]%2 == 0) {
            parostomb[paros] = tomb[i];
            paros = paros + 1;
            printf("paros: %d\n", parostomb[paros]);
        }
        else {
            paratlantomb[paratlan] = tomb[i];
            paratlan = paratlan + 1;
            printf("Paratlan:%d\n", paratlantomb[paratlan]);
        }
    }

    return 0;
}

Please help If you run the code you will be see the code is doesn't work normally

Upvotes: 0

Views: 58

Answers (1)

4386427
4386427

Reputation: 44274

Here

int parostomb[] = {};
int paratlantomb[] = {};

you do not set the array size.

You need to change it to:

int parostomb[sizeof(tomb)/sizeof(int)] = { 0 };
int paratlantomb[sizeof(tomb)/sizeof(int)] = {0 };

So what is sizeof (tomb)/sizeof(int) doing? It calculates the number of elements in the array tomb. The first part sizeof(tomb) calculates the number of bytes in the array. The second part sizeof(int) calculates the number of bytes in an int. Consequently, diving the first by the second will give you the number of int in the array (aka the number of array elements).

sizeof(tomb)/sizeof(int) can also be written sizeof tomb/sizeof *tomb. The later is typically considered the best because in case you to change the array type, e.g. use long int instead of int, you don't have to change sizeof(int) to sizeof(long int). When using sizeof *tomb the actual type is handled automatically.

Further here:

        parostomb[paros] = tomb[i];
        paros = paros + 1;
        printf("paros: %d\n", parostomb[paros]);

You increment paros before printing so you do not print the newly assigned value.

Change to:

        parostomb[paros] = tomb[i];
        printf("paros: %d\n", parostomb[paros]);
        paros = paros + 1;

Further here:

int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
...
int n = 9;  <-------------------- The array size is only 8

To avoid such bugs do:

int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
...
int n = sizeof(tomb)/sizeof(int);

Putting it all together, your code should be:

int main(void)
{
    int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
    int parostomb[sizeof(tomb)/sizeof(int)] = {0};
    int paratlantomb[sizeof(tomb)/sizeof(int)] = {0};
    int n = sizeof(tomb)/sizeof(int);
    int i = 0;
    int paros = 0;
    int paratlan = 0;
    
    for (i=0; i < n; i++){
        if (tomb[i]%2 == 0) {
            parostomb[paros] = tomb[i];
            printf("paros: %d\n", parostomb[paros]);
            paros = paros + 1;
        }
        else {
            paratlantomb[paratlan] = tomb[i];
            printf("Paratlan:%d\n", paratlantomb[paratlan]);
            paratlan = paratlan + 1;
        }
    }
}

Output

Paratlan:5
paros: 10
paros: 8
Paratlan:3
Paratlan:-1
paros: 4
paros: 2
Paratlan:9

That said... It seems strange that you save the numbers into two arrays that you never really use. It would make more sense to generate the arrays first and then print them in separate loops. Like:

int main(void)
{
    int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
    int parostomb[sizeof(tomb)/sizeof(int)] = {0};
    int paratlantomb[sizeof(tomb)/sizeof(int)] = {0};
    int n = sizeof(tomb)/sizeof(int);
    int i = 0;
    int paros = 0;
    int paratlan = 0;
    
    for (i=0; i < n; i++){
        if (tomb[i]%2 == 0) {
            parostomb[paros] = tomb[i];
            paros = paros + 1;
        }
        else {
            paratlantomb[paratlan] = tomb[i];
            paratlan = paratlan + 1;
        }
    }

    printf("Even numbers: ");
    for (i=0; i < paros; i++){
        printf("%d ", parostomb[i]);
    }
    printf("\n");

    printf("Odd numbers: ");
    for (i=0; i < paratlan; i++){
        printf("%d ", paratlantomb[i]);
    }
    printf("\n");
}

Output

Even numbers: 10 8 4 2 
Odd numbers: 5 3 -1 9 

Upvotes: 4

Related Questions