array problem!

E

engartte

Hi all,

I did the solution with consultation of some experts on the following
problem:

Declare an array of length of 10 and 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 example:
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


the solution is :


#include <stdio.h>

int main()
{
int a[10], temp;
int i,j;
for(i=0; i<10; i++){
a = i+1;
printf("%d ",a);
}
printf("\nType a number:");
scanf("%d", &temp);
for(i=0;i<x;i++){
for(j = 0; j<10; j++){
printf("%d ",a[j]);
}
printf("\n");
}
return 0;
}

now,I am asking you all experts that what is the another
more proper alternative way to pay for this problem,excpet
the follow-up solution?


tanx in advance


engartte
 
M

Michael Coyne

I did the solution with consultation of some experts on the following
problem:

[snip]
the solution is :


#include <stdio.h>

int main()
{
int a[10], temp;
int i,j;
for(i=0; i<10; i++){
a = i+1;
printf("%d ",a);
}
printf("\nType a number:");
scanf("%d", &temp);
for(i=0;i<x;i++){


I think you meant 'temp' instead of 'x' as it's not going to compile the
way it is now.


Michael
 
G

gooch

"now,I am asking you all experts that what is the another
more proper alternative way to pay for this problem,excpet
the follow-up solution?"

I am not sure what you are asking here. If you can clarify the
question maybe I can help.

The code you posted will not compile as is. The second for
loop contains a variable x that you never declared. I assume
from the code that it should be temp. I changed that and added
some comments and whitespace to make the code a little more
readable. You should get in the habit of using whitespace liberally
to break the code into logical components. This is not a big deal with
this small piece of code but as your programs get bigger it will be
much harder to read them without it.

#include <stdio.h>

int main()
{
int a[10], temp;
int i,j;

/*initialize and print the contents of the array*/
for(i=0; i<10; i++)
{
a = i+1;
printf("%d ",a);
}

/*get an int from the user*/
printf("\nType a number:");
scanf("%d", &temp);

/*print the contents of the array the number of times indicated by
the user*/
for(i=0;i<temp;i++) /*This temp was an x in your original post*/
{
for(j = 0; j<10; j++)
{
printf("%d ",a[j]);
}
printf("\n");
}

return 0;

}
 
B

Barry Schwarz

Hi all,

I did the solution with consultation of some experts on the following
problem:

Declare an array of length of 10 and 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 example:
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


the solution is :


#include <stdio.h>

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


Here you assign your values to the ten elements of the array. The
problem specification says you should read the values from the user
input.

You print the 10 values with no indication of what they are, with no
spacing between them, etc. Not a technical issue but a pretty poor
interface with the user.
printf("\nType a number:");

Since you don't want a \n after the instruction (though a space or two
would be nice), you should include a fflush(stdout) to insure the user
sees the instructions.
scanf("%d", &temp);
for(i=0;i<x;i++){

temp, not x.
for(j = 0; j<10; j++){

Please, please, please, learn to indent and do it consistently. Of
all the style issues ever discussed, this probably has the greatest
return on investment.
printf("%d ",a[j]);
}
printf("\n");
}
return 0;
}

now,I am asking you all experts that what is the another
more proper alternative way to pay for this problem,excpet
the follow-up solution?



<<Remove the del for email>>
 

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,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top