Write a C program to Remove even numbers and fill it with zeros.
Note: You can use only one loop for your logic.
Two variables apart from the array.
Input array 1,2,3,4,5,6
Output 1,3,5,0,0,0

Given a string , you have to predict whether it can be changed into a palindrome or not by changing the positions of the characters
Eg. Tests
Yes
Explanation:tests can be changed to tsest or stets

Given an array , find the nth largest and nth smallest number. Sorting should not be used

Print the pattern
N=4
4444444
4333334
4322234
4321234
4322234
4333334
4444444

(Partitioning and merging)
Given an array and a partition size, you have to partition the array with that value , then we will specify the partition order , you have to merge based on that order
Array : 1 2 3 4 5
Partition size 2
(so the array will be partitioned as 1 2, 3 4, 5)
Partition order 3 2 1
o/p: 5 3 4 1 2

Spiral matrix
Given a square matrix(n is odd) , print the elements in spiral order starting from the center till a[0][0]
A=
1 2 3
4 5 6
7 8 9
o/p 5 4 7 8 9 6 3 2 1

Lower triangle matrix
Given ā€œnā€ print the following
N=3
1
6 2
5 4 3

Find all possible longest increasing subsequences from an given array
Array 0 2 6 4 8 7
Ans :
Maxium length 4
0 2 6 8
0 2 4 8
0 2 4 7
0 2 6 7

So, you want us to do your homework for you? :-( Not going to happen! Make an honest effort and if you are still having problems, post the code here and we might decide to help you... DO NOT CHEAT!

commented: true!! +2

FWIW, these are basic CS math problems. See your professor if you need help understanding the problems.

brother, these aren't my homework problems.these are the questions which are asked in last interview. i couldnt code for these questions. so i posted these questions here. so that someone will comment the code. so that i can be able to know the logic behind these. Just one question for you--> if a student ask his teacher a doubt, it will be considered as a wrong thing huh ? am just a beginner. thanks for your reply in my post.

include<iostream.h>
#include<iostream.h>
main()
{
int n;
cin>>n;
int i,j,k=1;

int l=0,r=1,d=0,u=0;
int a[10][10];

for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=0;
i=0;
j=-1;
while(k<=(n*n))
{
if(r==1)
{
j++;
if(j==n || a[i][j]!=0)
{
r=0;
d=1;
j--;
}
else
{
a[i][j]=k;
k++;
}
}
else if(d==1)
{
i++;
if(i==n || a[i][j]!=0)
{
i--;
d=0;
l=1;
}
else
{
a[i][j]=k;
k++;
}
}
else if(l==1)
{
j--;
if(j<0 || a[i][j]!=0)
{
l=0;
j++;
u=1;
}
else
{
a[i][j]=k;
k++;
}

}
else if(u==1)
{
i--;
if(i<0 || a[i][j]!=0)
{
i++;
u=0;
r=1;
}
else
{
a[i][j]=k;
k++;
}
}
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
return 0;

}

this is my code for my spiral matrix but i didnt get the exact output, can u tell me how to develop this code to get the exact output.

#include<stdio.h>
#include<conio.h>

int main()
{
clrscr();
char c[30];

printf("Enter The String");
scanf("%s",c);
printf("\nSrting : %s",c);

int i=0,j=0,odd=0;
while(c[i]!='\0')
{
if(c[i]=='$')
{
i=i+1;
continue;
}
j=i+1;
while(c[j]!='\0')
{
if(c[i]==c[j])
{
c[i]='$';
c[j]='$';
printf("\nSrting : %s",c);
break;
}

/* if(c[++j]=='\0')
{
odd=odd+1;
printf("\nODD :%d",odd);
}
*/
j=j+1;
}
i=i+1;
}
i=0;
while(c[i]!='\0')
{
if(c[i]!='$')
odd=odd+1;
i=i+1;
}
printf("\n\n\n----------MAIN ANSWER------------");
if(odd<=1)
printf("\n\n STRING CAN BECOME PALINDROME");
else
printf("\n STRING CAN NOT BECOME PALINDROME");

getch();
return (0);
}

this is my code for my first question.

Member Avatar for iamthwee

Work on indenting your code properly for starters.

brother, these aren't my homework problems.these are the questions which are asked in last interview. i couldnt code for these questions. so i posted these questions here. so that someone will comment the code.

"These aren't homework questions, they're just questions I was given to do that I want other people to answer for me."

Sorry dude, but around here that falls under the category of homework. Do your own work. We'll help if you get stumped, but we won't do it for you.

I know answers for approximately 80% of the problems above. but it will be against rules if i help u without watching your effort in this. thanks. :-)

commented: 80% ? hmm.... +0

Code is helpful, but logic (often called pseudo-code) can be more so in order to see what your thought processes are in solving the problems.

dont worry sureshshan1292....
surely a person with helping tendency will surely help you...
helping others is not at all a abd thing...
he clearly stated that he is not able to solve those...
then whats wrong in it...

Write a C program to Remove even numbers and fill it with zeros.
Note: You can use only one loop for your logic.
Two variables apart from the array.
Input array 1,2,3,4,5,6
Output 1,3,5,0,0,0

This is the implementation in Java... Hope this will help... :-)

public class RemoveEven {
    public static void main(String[] args) {
        int[] arr ={1,2,3,4,5};
        int k=0;
        for(int i=0;i<arr.length;i++) {
            if(arr[i]%2!=0) {
                arr[k]=arr[i];
                k++;
            }
        }
        for(;k<arr.length;k++)
            arr[k]=0;
        for(int i:arr) System.out.print(i+" ");
    }
}

sureshshan1292, in the spiral problem, you should be hearing warning sirens that your algorithm is too (low-level in the logic, explicit, or specific), and not abstracted enough. That is a LOT of code!

Are you still interested in this problem, or did you figure out what you wanted, already?

/*
 * @author suresh varman
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int[] arr ={5,4,3,2,1};
        int k=0;
        for(int i=0;i<arr.length;i++) {
            if(arr[i]%2!=0) {
                arr[k]=arr[i];
                if(i>0)arr[i]=0;
                k++;
            }
            else
            {
                arr[i]=0;
            }

        }
         for(int d=0;d<arr.length;d++)
             {
                 System.out.println(arr[d]);
             }
    }
}

Here is my code for spiral.

#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    int i,j,k,m,n,l,v=0,c=1;
    cout<<"Enter the order of the matrix:";
    cin>>n;
    int a[n][n];
    k=n/2;
    l=k;
    m=k;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cin>>a[i][j];

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cout<<a[i][j];

        }
        cout<<endl;
    }

    cout<<a[k][l];
    while((l!=0)||(k!=0))
    {

    //left()

        for(i=1;i<=c;i++)
        {
        l--;
        cout<<a[k][l];
        if((l==0)&&(k==0))
    {
    goto end;
}

    }


    //down()

        for(i=1;i<=c;i++)
        {
            k++;
            cout<<a[k][l];
        }

    //right()

        for(i=1;i<=c+1;i++)
        {
            l++;
            cout<<a[k][l];

        }

    //  up()
            for(i=1;i<=c+1;i++)
        {
            k--;
            cout<<a[k][l];
            }
            c++;
            c++;




}
end:
    return 0;
}

This is the implementation in Java... Hope this will help.

It won't because your program doesn't meed the requirements of the problem. The program can only contain one loop.

Write a C program to Remove even numbers and fill it with zeros.
Note: You can use only one loop for your logic.
Two variables apart from the array.
Input array 1,2,3,4,5,6
Output 1,3,5,0,0,0

I think this suits the problem. I used goto instead of a loop. I know goto is not a good option but I think there is no harm in using it for small programs

#include<stdio.h>
int ar[10];  //array
int main() {
    int i = 0;  //1st element
    int temp;   //2nd element
    printf("enter the numbers : ");
    while(i < 5) {
        scanf("%d",&temp);
        if(temp%2 == 0) {
        }
        else {
            ar[i] = temp;
            i++;
        }
        if(i == 5) {
            i = 0;
            XY:
                if(i<10)
                    printf("%d, ",ar[i]);
                else
                    break;
                i++;
                goto XY;

        }
    }
    return 0;
}

I know goto is not a good option but I think there is no harm in using it for small programs

The problem with goto is it's easy to get into the habit of using it as a crutch for not thinking about your code in a structured manner.

I think this suits the problem. I used goto instead of a loop

Nope. goto can be used a just another type of loop.

Nope. goto can be used a just another type of loop.

then, can we use recursion for this?

Nope. goto can be used a just another type of loop.

I think its okay now!

#include<stdio.h>
int ar[10],i = 0;

int main() {
    int temp;
    printf("enter the numbers : ");
    while(i < 10) {
        if(i >= 5) {
            printf("%d, ",ar[i]);
            i++;
            continue;
        }
        scanf("%d",&temp);
        if(temp%2 == 0) {
        }
        else {
            ar[i] = temp;
            printf("%d, ",ar[i]);
            i++;
        }
    }
    return 0;
}

Attempted the things others attempted..

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

void        remove_even         (int data[], const unsigned int size);
void        print_spiral        (const unsigned int size);
static void print_spiral_line   (const unsigned int n, const unsigned int line);
bool        can_be_palindrome   (char text[], const unsigned int size, const bool middle_found);

/**
 *  Write a C program to Remove even numbers and fill it with zeros.
 *  Note: You can use only one loop for your logic.
 */
void remove_even(int data[], const unsigned int size)
{
    int i          = 0,
        pivot      = size - 1;

    while (i < size && i <= pivot)
    {
        // This number is even.
        if (data[i] % 2 == 0)
        {
            // Switch it with the last.
            data[i]     = data[pivot];
            data[pivot] = 0;

            // An element was added to "the zero section".
            pivot--;
        }
        else {
            i++;
        }
    }
}


/**
 *  Print the pattern
 *
 *   N=4
 *   4444444
 *   4333334
 *   4322234
 *   4321234
 *   4322234
 *   4333334
 *   4444444
 */
void print_spiral(const unsigned int size)
{
    int i                  = 0;
    const int SPIRAL_WIDTH = (2 * size) - 1;

    if (size > 0)
    {
        // Print all lines of the spiral.
        for (i = 0; i < SPIRAL_WIDTH; i++)
        {
            print_spiral_line(size, i);
            printf("\n");
        }
    }
}


static void print_spiral_line(const unsigned int n, const unsigned int line)
{
    int i                  = 0;
    const int SPIRAL_WIDTH = (2* n) - 1;

    // The first and the last line of a spiral are easy.
    if (line == 0 || line == SPIRAL_WIDTH - 1)
    {
        for (i = 0; i < SPIRAL_WIDTH; i++)
            printf("%d", n);
    }
    // Other lines consists of inner spiral lines of other sizes.
    else {
        printf("%d", n);
        print_spiral_line(n - 1, line - 1);
        printf("%d", n);
    }
}


/**
 *  Given a string , you have to predict whether it can be changed into
 *  a palindrome or not by changing the positions of the characters
 *  Eg. Tests
 *  Yes
 *  Explanation:tests can be changed to tsest or stets
 */
bool can_be_palindrome(char text[], const unsigned int size, const bool middle_found)
{
    int  i      = 0;
    char symbol = 0,
         temp   = 0;

    // Empty strings are palindromes
    if (size > 0)
    {
        // Start at the first symbol.
        symbol = text[0];

        // Find a matching symbol.
        for (i = 1; i < size; i++)
        {
            // We've found a matching symbol.
            if (text[i] == symbol)
            {
                // swap [i] with [1] (if they aren't the same)
                if (i != 1)
                {
                    temp    = text[1];
                    text[1] = text[i];
                    text[i] = temp;
                }
                return can_be_palindrome(text + 2, size - 2, middle_found);
            }
        }

        // No match was found.
        // If the size of the string is uneven, and we haven't found
        // an unmatched character yet, it can still be a palindrome.
        if (!middle_found && size % 2)
        {
            return can_be_palindrome(text + 1, size - 1, true);
        }
    }
    return (size == 0);
}

int main(void)
{
    int i = 0;

    char input3[]   = "tests";
    int  input2[] = {12, 43, 429, 283, 283, 192, 12, 384, 11, 2, 392};
    const int INPUT2_SZ = sizeof(input2) / sizeof(int);
    const int SPIRAL_SZ = 4;

    printf("1. Removing even values from an array.\n");
    printf("Original values: ");

    for (i = 0; i < INPUT2_SZ; i++)
        printf("%d ", input2[i]);
    printf("\n");

    remove_even(input2, INPUT2_SZ);

    printf("Values after function call: ");

    for (i = 0; i < INPUT2_SZ; i++)
        printf("%d ", input2[i]);
    printf("\n\n");

    printf("2. Printing a spiral of size %d.\n\n", SPIRAL_SZ);
    print_spiral(SPIRAL_SZ);
    printf("\n");

    printf("3. Determining if \"%s\" can be a palindrome.\n", input3);

    if(can_be_palindrome(input3, strlen(input3), false))
        printf("It can.\n");
    else
        printf("It can't.\n");

    return 0;
}

Got bored and did another one..

#include <stdio.h>
#include <assert.h>

#define ARRAY_SIZE (5)

/**
  * Spiral matrix
  * Given a square matrix(n is odd) , print the elements
  * in spiral order starting from the center till a[0][0]
  * A=
  * 1 2 3
  * 4 5 6
  * 7 8 9
  * o/p 5 4 7 8 9 6 3 2 1
*/
void print_as_spiral(const int data[][ARRAY_SIZE], const unsigned int size)
{
    assert (size % 2 != 0);

    int i = 0;

    if (size == 1) {
        printf("%d ", data[0][0]);
    } else {
        print_as_spiral(data[1] + 1, size - 2);

        for(i = 1; i <= size - 1; i++) printf("%d ", data[i]       [0]);
        for(i = 1; i <  size - 1; i++) printf("%d ", data[size - 1][i]);
        for(i = size - 1; i >  0; i--) printf("%d ", data[i]       [size - 1]);
        for(i = size - 1; i >= 0; i--) printf("%d ", data[0]       [i]);
    }
}



int main(void)
{
    const int data[][ARRAY_SIZE] ={{ 1,  2,  3,  4, 5},
                                   {16, 17, 18, 19, 6},
                                   {15, 24, 25, 20, 7},
                                   {14, 23, 22, 21, 8},
                                   {13, 12, 11, 10, 9}};

    print_as_spiral(data, ARRAY_SIZE);

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.