Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <iostream>
using namespace std;

int main()
{
int n,i;
cin>>n;
int arr1[n+1][n+1],arr2[n+1][n+2],arr3[n+1][n+3];
for(int i=1;i<=n;i++)
{
    arr1[i][i]=n;
}
int pos;
for(int i=1;i<=n;i++)
{
    pos=1;
    int temp=n;
    while(pos!=1)
    {
        arr1[i][pos-1]=temp-2;
        temp=temp-2;
        pos--;
    }
    temp=n;
    pos=i;
    while(pos!=n)
    {
        arr1[i][pos+1]=temp-2;
        temp=temp-2;
        pos++;
    }
}
int temp2=n+(n-1)*2;
for(int i=1;i<=n;i++)
{
    int loop=temp2;
    int step=0;
    int step2=0;
    for(int j=1;j<=n;j++)
    {
        if (loop>n)
        {
            arr2[i][j]=temp2-step;
            step+=2;
            loop-=2;
        }else if (loop<n)
        {
            arr2[i][j]=temp2-step+step2;
            step2+=2;
        }else
        {
            arr2[i][j]=n;
            loop-=2;
            step2+=2;
        }
    }
    temp2-=2;
}
int temp3=n+(n/2)-1;
for(int j=1;j<=n/2;j++)
{
    arr3[i][j]=temp3;
    temp3--;
}
cout<<"\ntype 1"<<endl;
for(int i=1;i<=n;i++)
{
    for(int j=1;j<=n;j++)
    {
        cout<<arr1[i][j]<<"";
    }
    cout<<endl;
}
cout<<"\ntype 2"<<endl;
for(int i=1;i<=n;i++)
{
    for(int j=1;j<=n;j++)
    {
        cout<<arr2[i][j]<<"";
    }
    cout<<endl;
}
cout<<"\ntype 3"<<endl;
for(int k=1;k<=3;k++)
{
    for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=n/2;j++)
        {
            cout<<arr3[1][j]<<"";
        }
    }
    cout<<endl;
}
for(int k=1;k<=3;k++)
{
    for(int i=1;i<=2;i++)
    {
        for(int j=n/2;j>=1;j--)
        {
            cout<<arr3[1][j]<<"";
        }
    }
    cout<<endl;
}
    return 0;
}


What I have tried:

#include <stdio.h>

int main()
{
int n,i;
printf("enter number");
int arr1[n+1][n+1],arr2[n+1][n+1],arr3[n+1][n+1];
for(int i=1;i<=n;i++)
{
    arr1[i][i]=n;
}
int pos;
for(int i=1;i<=n;i++)
{
    pos=1;
    int temp=n;
    while(pos!=1)
    {
        arr1[i][pos-1]=temp-2;
        temp=temp-2;
        pos--;
    }
    temp=n;
    pos=i;
    while(pos!=n)
    {
        arr1[i][pos+1]=temp-2;
        temp=temp-2;
        pos++;
    }
}
int temp2=n+(n-1)*2;
for(int i=1;i<=n;i++)
{
    int loop=temp2;
    int step=0;
    int step2=0;
    for(int j=1;j<=n;j++)
    {
        if (loop>n)
        {
            arr2[i][j]=temp2-step;
            step+=2;
            loop-=2;
        }else if (loop<n)
        {
            arr2[i][j]=temp2-step+step2;
            step2+=2;
        }else
        {
            arr2[i][j]=n;
            loop-=2;
            step2+=2;
        }
    }
    temp2-=2;
}
int temp3=n+(n/2)-1;
for(int j=1;j<=n/2;j++)
{
    arr3[i][j]=temp3;
    temp3--;
}
printf("\ntype 1");
for(int i=1;i<=n;i++)
{
    for(int j=1;j<=n;j++)
    {
        printf("%d",arr1[i][j]);
    }
    printf("\n");
}
printf("\ntype 2");
for(int i=1;i<=n;i++)
{
    for(int j=1;j<=n;j++)
    {
        printf("%d",arr2[i][j]);
    }
    printf("\n");
}
printf("\ntype 3");
for(int k=1;k<=3;k++)
{
    for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=n/2;j++)
        {
            printf("%d",arr3[i][j]);
        }
    }
    printf("/n");
}
for(int k=1;k<=3;k++)
{
    for(int i=1;i<=2;i++)
    {
        for(int j=n/2;j>=1;j--)
        {
            printf("%d",arr3[i][j]);
        }
    }
    printf("/n");
}
    return 0;
}
Posted
Updated 11-May-21 9:16am

You've correctly replaced iostream with stdio.h, and the rest of it looks like C to me. The only thing missing in your new code appears to be a replacement for cin >> n, and you should be able to find one in stdio.h.
 
Share this answer
 
Comments
[no name] 11-May-21 8:10am    
thank you
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

But that pile of "code"? It's C code already, with some C++ I/O (cin / cout) so it's trivial to "convert it".
Is it worth converting? No. It's poor quality student grade homework and unless it's supposed to answer the same homework question you are, it's not worth the bother. Even if it is, it's not going to get you a good grade ... it's really that bad a standard of code!
 
Share this answer
 
Comments
OriginalGriff 11-May-21 6:44am    
Means absolutely nothing to me - I assume it's "thank you very much" in your native language?
CPallini 11-May-21 6:13am    
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