Status
Not open for further replies.

hayabusa_ryu

Journeyman
Hello Friends,

I m a beginner C programmer.When I was reading a book,I found two questions which could be solved by me.They are :-
Q(1).--I have to take input of any 10 numbers and print the two largest numbers of them.For example,if the input r :- 3,4,4,5,1,4,7,2,1,0.It should print 7 & 5.I have to make this program using only if,if-else or while statements.
Q(2).(a)--Another Problem is that,I have to take input for side of squares between 1-20 numbers and print them as asterisk filled square.Use only if,if-else,while statemens.Eg:-
If I input no.4 then the output should be:-
****
****
****
****
If I input no.2 then it should printl like this:-
**
** and so on.
(b).--This Question is similar to Q2(a) but the difference is that it should print like this when asked to input no. 4
****
<space>****
****
<space>****
and if input is 6 then it print
******
<space>******
******
<space>******
******
<space>******
and so on.
 
Last edited:

QwertyManiac

Commander in Chief
Both your programs are very easy to solve. Just use a bit of logic :)

For the first one you can use while() to get input of 10 numbers (While index is less than 10, get input and store in array[index] and increment index).

Next, sort the array via a normal sorting method and display the last two elements of the array.

Here's the code to illustrate it:
Code:
#include<stdio.h>

int main()
{
    int a[10];
    int i=0, j=0, temp=0;
    printf("Enter the numbers: \n");
    // Gets the Input
    while(i<10)
    {
        scanf("%d",&a[i]);
        i++;
    }
    // Resets the Counter for Further Operations
    i=0;
    // For the entire array,
    while(i<10)
    {
        // For each value behind the chosen a[index],
        while(j<i)
        {
            // Swap if lesser
            if(a[i]<a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
            // Increment lagging counter
            j++;
        }
        // Increment leading counter
        i++;
    }
    // Print the two largest numbers from the Ascend-sorted array
    // thus created.
    printf("Two large numbers are: %d and %d\n",a[9],a[8]);
    return 0;
}
About the second program's A part, I don't understand the pattern, its sort of relative somehow, but I have no idea.

The B part is simple again. Assume it to be a two dimensional square matrix and print it that way. Like the code below does:
Code:
#include<stdio.h>

int main()
{
    int a, i=0, j=0;
    
    printf("Enter any number between 1 and 20: ");
    scanf("%d",&a);
    
    if(a<=20)
    {

        while(i<a)
        {
            if((i+1)%2==0)
            printf(" ");
            j=0;
            while(j<a)
            {
                printf("*");
                j++;
            }
            printf("\n");
            i++;
        }
    }
    
    else
    printf("Enter only between 1 and 20\n");
    
    return 0;
}
Edit: Updated code block #2 to add spaces as changed in OP's Post.
 
Last edited:
OP
hayabusa_ryu

hayabusa_ryu

Journeyman
Thank U very much QwertyManiac.
But these r problems from the 3rd chapter of the C book(How to Program C) where there is no sign of using arrays.Can these questions be solved without using arrays? I've to solve these questions without using arrays.
In the Q2(a),we have to take input of side of a suqare upto 10 numbers(1 to 10).Suppose when asked to input a number like this:-
Enter the side of the square : 5 (this no. is given by me)

Then it print square or whatever with asterisks filled means 5 x 5 = 25 asterisks(5 asterisks per line upto 5th line). Eg:-

*****
*****
*****
*****
*****
Or if the inputed no. was 8 then it print the result/output in the form(8 asterisks per line upto 8th line) :-
********
********
********
********
********
********
********
********
understood!!
Try to make program without using array!!
 

fun2sh

Pawned!... Beyond GODLIKE
here its my dear

Code:
#include<stdio.h>
void main()
{clrscr();
int s;
printf("\n enter the size ");
scanf("%d",&s);
for(int i=1;i<=s*s;i++)
{printf("*");
if(i%s==0)
printf("\n");
}

getch();
}
 
Last edited:

QwertyManiac

Commander in Chief
You had put only 3 lines in the square of value 4 originally in your above question. Thus I was confused at your attempt to make it look like a square. :)

And here's your program (1) without arrays nor pre-assigned constants.
Code:
#include<stdio.h>

int main()
{
	int a,b,in,i=0;
	
	while(i<10)
	{
		printf("Enter the value #%d: ",i+1);
		scanf("%d",&in);
		if(i>1)
		{
			if(in>a)
			{
					b=a;
					a=in;
					i++;
					continue;
			}
			
			if(in>b)
			{
				b=in;
			}
		}
		
		else
		{
			if(i==0)
			a=in;
			if(i==1)
			{
				b=in;
				if(b>a)
				{
					in=a;
					a=b;
					b=in;
				}
			}
		}
		
		i++;
	}
	
	//Optional and perhaps redundant
	//Swap a and b to get it in order, if not.
	if(a<b)
	{
		in=a;
		a=b;
		b=in;
	}
	
	printf("\nTwo large numbers are: %d and %d\n",a,b);
	
	return 0;
}

And for the second one's (A):
Code:
#include<stdio.h>

int main()
{
        int a, i=0, j=0;

        printf("Enter any number between 1 and 20: ");
        scanf("%d",&a);

        if(a<=20)
        {
                while(i<a)
                {
                        j=0;
                        while(j<a)
                        {
                                printf("*");
                                j++;
                        }
                        printf("\n");
                        i++;
                }
        }

        else
        printf("Enter only between 1 and 20\n");

        return 0;
}

Hope you understand the first block, took me a while to make it without presets.

fun2sh said:
here its my dear

#include<stdio.h>
void main()
{clrscr();
int s;
printf("\n enter the size ");
scanf("%d",&s);
for(int i=1;i<=s*s;i++)
{printf("*");
if(i%s==0)
printf("\n");
}

getch();
}
No for loops, just while and if/if-else :)
 
Last edited:
OP
hayabusa_ryu

hayabusa_ryu

Journeyman
Yeah,you r right Mr. QwertyManiac.I made typing mistake there.Thank U for pointing my mistake.
Again,thanx to all for paying attention here and posting reply for my thread.Now I can understand and solve these questions very easily.I really welcome and admire ur response.:)
 
Last edited:
Status
Not open for further replies.
Top Bottom