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 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
"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;
}
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>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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))
...
|
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...
|
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...
|
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...
|
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...
| |
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 =...
|
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...
|
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...
|
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...
|
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)...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |