Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C++
for(int i=0; i<5;i++)
	cin>>a[i];

        for(int w=0;w<5;w++){
        for(int y=0;y<5;y++){
        if(a[w]*(y+1)==a[y]){
         cout<<"A"<<"  ";
        else
        cout<<a[w]*(y+1)<<"   ";
        }
        }
        cout<<endl;
        }


sample input: 1 2 3 4 5                               
target output                           my output
A A A A A                               A A A A A
A A 6 8 10                              2 4 6 8 10
A 6 9 12 15                             3 6 9 12 15
A 8 12 16 20                           4 8 12 16 20
A 10 15 20 25                           5 10 15 20 25


What should i do?
Posted
Updated 6-Feb-15 6:36am
v2
Comments
nv3 6-Feb-15 13:19pm    
Please elaborate on what you are trying to do and what your problem is.
jeron1 6-Feb-15 13:47pm    
Have you tried this algorithm on paper?
Sergey Alexandrovich Kryukov 6-Feb-15 14:02pm    
What is "a number in array"? And index in array, or something else? What number, equal to what?
—SA

1 solution

I think from your problem statement, you need to search the array. I do not see in your code that you searched the array. I think you indexed into the array.

C++
#include <stdio.h>
#include <iostream>
using namespace std;


void original(void)
{
    int a[] =  { 1, 2, 3, 4, 5 };
    
    for(int w=0;w<5;w++)
    {
        printf("Row %i, w = %i:   ", w+1, w);

        for(int y=0;y<5;y++)
        {
            printf("\n   Col %i: {a[w] = %2i, a[w]*(y+1) = %2i, a[y] = %2i} ", y, a[w], a[w]*(y+1), a[y]);
            if(a[w]*(y+1)==a[y])
                cout<<"A"<<"   ";
            else
                cout<<a[w]*(y+1)<<"   ";
        }
        cout<<endl;
    }
}


void new_version(void)
{
    /*
    Sounds from your problem statement like you want to SEARCH
    the array.
    */
    const int end_of_array = -1;

    int a[] =  { 1, 2, 3, 4, 5, end_of_array},
        i = 0, // an index
        y = 0;

    for(int w=0;w<5;w++)    // as in original
    {
        for(y=0; y<5; y++)  // as in original
        {
    
            for(i=0; a[i] != end_of_array; i++)     // SEARCH the array
            {
                if(a[w]*(y+1)==a[i])                // test for found
                    /* found */ break;
            }

            if(a[i] == end_of_array)            // if index after end of array, then not found. Otherwise, found.
                cout<<a[w]*(y+1)<<"   ";        // not in array
            else
                cout<<"A"<<"   ";               // in array

        }
        cout<<endl;
    }
}



int main(int argc, char *argv[])
{
    original();

    cout<<endl<<"==========================="<<endl;

    new_version();

    getchar();

    return 0;
}

</iostream></stdio.h>
 
Share this answer
 
Comments
enhzflep 6-Feb-15 15:09pm    
Seems okay. I'd avoid using a sentinel value myself - unless you know that the chosen value can never appear in the input. I'd also avoid mixing printf and cout - likely just going with printf in this example as a means of making the program's .exe much smaller. It's not like we need any of the functionality offered by ostream in this case anyway.

I'd change the sentinel-based code from

for(i=0; a[i] != end_of_array; i++) to for(i=0; i<5; i++)

and

if(a[i] == end_of_array) to if (i == 5)

Naturally, if the input was of variable length I'd use a variable in place of the hard-coded value of 5.

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