By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Problem on Array

P: n/a
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.

Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"engaref" <na*****@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.
Nov 14 '05 #2

P: n/a
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[i]);
}
for(i=0; i<10; i++){
printf("%d",a[i],a[0] = a[1]);
}
return 0;
}

Nov 14 '05 #3

P: n/a
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
"

engaref wrote:
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[i]);
You are not using the return value of scanf().
}
for(i=0; i<10; i++){
printf("%d",a[i],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]=[i];
for (i=0; i<10; i++)
printf("%d ",a[i]);
}
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[i]);
}

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

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

return 0;
}

--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #4

P: n/a
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,

Nov 14 '05 #5

P: n/a
engaref wrote:
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?
Nov 14 '05 #6

P: n/a
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

Nov 14 '05 #7

P: n/a
Michael Mair <Mi**********@invalid.invalid> writes:
[...]
Thus: Do the exchange first, then do the output:

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

[...]

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8

P: n/a


Keith Thompson wrote:
Michael Mair <Mi**********@invalid.invalid> writes:
[...]
Thus: Do the exchange first, then do the output:

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


[...]

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

*g* Too hasty...

Thanks for mentioning :-)
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.