Problem on Array

E

engaref

Hello Every body,
I am new with C programming.I have received the Problems from my
advisor on Array
but I did not find any Proper answer yet.
If Possible,please make a solution for the Problems.

Thanks

The Problems are as follows:
1-Declare an array of length of 10 and read integers into the elements
of array from keyboard(using Scanf).
Then exchange the preceding two elemnt of the array and print the
contents.
Out put Example:
1 2 3 4 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10

2-Similear as above,but this time insert the first element of the array
into the fifth position and print the contents.
Output Ex:
1 2 3 4 5 6 7 8 9 10
2 3 4 5 1 6 7 8 9 10

3-Similear as above,but insert the first element of the array into the
position specified by a following integer and print the contents.
output Ex:
1 2 3 4 5 6 7 8 9 10
7
3 5 7 9 2 4 1 6 8 10


4-Declare an array of Length of 10 an read integers into the elements
of the array from keyboard.then read an integer which specifies the
number of repetition.
according to the repetition number,print the contents of the array.

Output Ex:
1 2 3 4 5 6 7 8 9 10
3
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10


5- Declare a two-dimensional array "a" of size 10 by 10 and read 10
integers into a[0][0] to a[0][9].then repeat
insertion operation specified by an integers sequence nin times and
store the results from a[1] to a[9].
Finally,print the array.
 
D

dandelion

engaref said:
Hello Every body,
I am new with C programming.I have received the Problems from my
advisor on Array but I did not find any Proper answer yet.

That's too bad.

Unfortunately, people around here are much too busy to do *your* homework.
As soon as you have at least *tried* that, people will be unwilling to help
you. First show us what solution you arrived at, or what you have tried.

The problems do not seem overly complicated.
 
E

engaref

Hi again,
Of Course,this is not the homwork.thius is just for my undestanding and
Practice.
because,I have no experinces with C specailly with Arrays.
By the way,I tried as follows,but is wrong:
#include <stdio.h>

main()
{

int a[10];
int i;


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


for(i=0; i<10; i++){
printf("%d",a,a[0] = a[1]);
}
return 0;
}
 
M

Michael Mair

Please quote enough context that we know what you are referring to.

Guess:
"
1-Declare an array of length of 10 and read integers into the elements
of array from keyboard(using Scanf).
Then exchange the preceding two elemnt of the array and print the
contents.
Out put Example:
1 2 3 4 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10
"
Hi again,
Of Course,this is not the homwork.thius is just for my undestanding and
Practice.
because,I have no experinces with C specailly with Arrays.
By the way,I tried as follows,but is wrong:
#include <stdio.h>

main()
main() must return int. You return int. But you do not give a return
type for main(). Correct:

int main ()
or
int main (void)

Look for compiler warnings. If there are none, get another
compiler.
{

int a[10];
int i;


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


You are not using the return value of scanf().
}


for(i=0; i<10; i++){
printf("%d",a,a[0] = a[1]);


Why exactly are you doing that?
You tell printf() with "%d" that you are giving _one_ further argument.
You provide _two_.
a[0] = a[1]
has the effect that the first two array elements are identical.
You want to exchange them. That is, you want to print the "original"
a[0] when i==1.
Thus: Do the exchange first, then do the output:

i=a[0]; a[0]= a[1]; a[1]=;
for (i=0; i<10; i++)
printf("%d ",a);
}
return 0;

without output of '\n' before exiting the program, it is possible
that you will not get any output at all.

Cheers
Michael



____arr1.c_______

#include <stdio.h>

int main (void)
{
int a[10], tmp;
int i;

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

putchar('\n');
for(i=0; i<10; i++){
printf("%d ",a);
}
putchar('\n');

tmp = a[0]; a[0] = a[1]; a[1] = tmp;
for(i=0; i<10; i++){
printf("%d ",a);
}
putchar('\n');

return 0;
}
 
E

engaref

Dear Mr.Michael,

Thanks a lot for your kind reply and advice.
I understood some on Array by using yours explaination now.
Thanks and Regards,
 
I

infobahn

engaref said:
Hello Every body,
I am new with C programming.I have received the Problems from my
advisor on Array
but I did not find any Proper answer yet.
If Possible,please make a solution for the Problems.

If you get other people to do your homework for you, you're going to
stay new with C programming for quite some time.

Don't you agree that you'd learn far more if you tried these for
yourself, and then asked for help on matters that you are "stuck"
with?
 
E

engaref

Hi,
Thanks for advice.
These are not home work.these are all for my practice.I need help and
advice more.
this is the first time that I am learning the Programming.these are all
new for me.

bye
 
K

Keith Thompson

Michael Mair said:
Thus: Do the exchange first, then do the output:

i=a[0]; a[0]= a[1]; a[1]=;
for (i=0; i<10; i++)
printf("%d ",a);

[...]

And finding the error in the above code is a good exercise.
 
M

Michael Mair

Keith said:
Thus: Do the exchange first, then do the output:

i=a[0]; a[0]= a[1]; a[1]=;
for (i=0; i<10; i++)
printf("%d ",a);


[...]

And finding the error in the above code is a good exercise.

*g* Too hasty...

Thanks for mentioning :)


Cheers
Michael
 

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

No members online now.

Forum statistics

Threads
474,157
Messages
2,570,879
Members
47,413
Latest member
KeiraLight

Latest Threads

Top