0

So I have 3 int type arrays, A, B and C. The program checks array A elements for prime numbers, if the number is prime in array it gets copied to array B, if not array C. Later it prints all three arrays.

This is the code piece a have problems with:

    if(onalg)
    {
    B[i]=A[i];  
    }
    else
    {
    C[i]=A[i]; 
    }

Then sort the array B and C using qsort ( done this, works ). After all that it has to print all arrays like this:

for(i=0;i<n;i++)
{
    printf("%d        %d        %d\n", A[i],B[i], C[i]);
}

How can I make it print empty spots in array? Currently it prints some random numbers in array C[i] spot when B[i]=A[i] and in array B[i] when C[i]=A[i].

4 Answers 4

1

You cannot.
You will need to assign some value to your array member which will indicate it is not initialized and then check for that value and treat it as blank while printing it out.

if(onalg)
{
    B[i]=A[i];  
    C[i]= //Some place holder value indicating unfilled;
}
else
{
    C[i]=A[i]; 
    B[i]= //Some place holder value indicating unfilled;
}
Sign up to request clarification or add additional context in comments.

2 Comments

What is that placeholder that indicates the array is unfilled? I have searched everywhere, can't find anything that can do that :S
@user1092141: Place holder means a value which you decide will never be a valid value in your array(say -999) and then you check that while printing to know when to print a blank.
1

Either initialize your arrays B and C before you start filling them in (with memset for instance), or set them both in each conditional.

if(onalg)
{
    B[i]=A[i];  
    C[i]=-1;  // or some other constant
}
else
{
    C[i]=A[i]; 
    B[i]=-1;  // or some other constant
}

If you don't have a constant you can use as a "guard", consider using two more arrays, properly initialized, that store whether that index in B and C resp. is set or not.

You'll need more logic in your display code either way to print out a blank for "unset" values.

Comments

0
CString strA, strB, strC;

for(i=0;i<n;i++)
{
    if (A.GetLength() > i+1)
        strA = Format(%d, A[I]);
    else
        strA = "";

    if (B.GetLength() > i+1)
        strB = Format(%d, B[I]);
    else
        strB = "";

    if (C.GetLength() > i+1)
        strC = Format(%d, C[I]);
    else
        strC = "";

    printf("%s        %s        %s\n", strA, strB, strC);
}

Comments

0

When you want it to be empty, you can set the B[i] or C[i] to 0, and then print it one by one by checking if the value is 0.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.