473,503 Members | 10,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array problem!

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] = i+1;
printf("%d ",a[i]);
}
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

Nov 14 '05 #1
3 1250
On Tue, 04 Jan 2005 11:34:23 -0800, engartte said to the parser:
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] = i+1;
printf("%d ",a[i]);
}
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
Nov 14 '05 #2
"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] = i+1;
printf("%d ",a[i]);
}

/*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;

}

Nov 14 '05 #3
On 4 Jan 2005 11:34:23 -0800, "engartte" <na*****@hotmail.com> wrote:
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] = i+1;
printf("%d ",a[i]);
}
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>>
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3235
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
9
4765
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
5
2190
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
8
4593
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
12
5485
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
204
12889
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
8
10684
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
104
16852
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
23
7366
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
1969
by: assgar | last post by:
Hi Seasons Greetings Its back, I am being haunted. I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2)...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.