Reading the data from keyboard!

J

John

Hi all,

I got totally confusion on the following code:


#include <stdio.h>

#define MAX 1000

main()
{
int n, a[MAX], i, j;

scanf("%d", &n);

for (i = 0; i < n; i++)
scanf("%d", &a);

/* find the index j here */

printf("%d\n", j);
}



I want to get the following outputs:
For example:

5 3 2 1 4 5
3


or:
6 6 3 1 4 2 5
5

or:
4 3 2 1 4
3


or"

10 10 9 8 7 6 5 4 3 2 1
10



In fact, I am going to solve this problem by the
follow-up code,but it has some mistake.

-----------------------------------------------------

The Problem says:

[Write a C program that reads the number of data "n" and
the data themselves "d1", "d2", ...,"dn" from the keyboard
and stores them into an array.
Then it finds the index "j"that satisfies the inequations:

d(j-1) > dj < d(j+1) < ... < d(n-1) < dn

and reports "j".]
----------------------------------------------------------


So,If you know the proper way to pay for this solutions,
please show them with kindness.

Tanx in advance!

John
 
S

S.Tobias

John said:
I got totally confusion on the following code:

What does confuse you? You haven't actually told us.
#include <stdio.h>
#define MAX 1000
main() int main()
{
int n, a[MAX], i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &a);

/* find the index j here */
printf("%d\n", j);
}

Your code seems to me generally correct.

I want to get the following outputs:
For example:
[snip examples]

In fact, I am going to solve this problem by the
follow-up code,but it has some mistake.

Why haven't you sent it here then?
-----------------------------------------------------
The Problem says:
[Write a C program that reads the number of data "n" and
the data themselves "d1", "d2", ...,"dn" from the keyboard
and stores them into an array.
Then it finds the index "j"that satisfies the inequations:
d(j-1) > dj < d(j+1) < ... < d(n-1) < dn
and reports "j".]

Hints: d(j) is the minimal element in the range [(j-1), n].
There might be more than one solution. Perhaps the fastest
(and easiest) way is to search backwards from the end.
----------------------------------------------------------

This is an algorithms problem and is off-topic in c.l.c.
See "Welcome ..." message for directions.
So,If you know the proper way to pay for this solutions,
please show them with kindness.

The proper way to solve the assignment would be: sit down and
solve it yourself. A pencil and paper might help.
 
G

Giesor

Interesting advice!
he wrote the problem to make the matter clear.
he may do not expect the "fun" from yours side:
sit down and
solve it yourself. A pencil and paper might help.

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,481
Latest member
Arnulfo32P

Latest Threads

Top