I have one C programming problem:
Enter value, 0 to stop: 15
26
45
0
List now : 15 26 45 0 37814140
Value to change : 26
Want to change with value: 45
List now: 15 45 26 0 37814140
The problem is when I key in 0, it should stop, and the list only can show up to 45 only.
Why my output show 0 and 37814140, izzit my code is worng?
Below is my code: - #include <stdio.h>
-
#define max 5
-
int main()
-
{
-
int num[max], i=0;
-
int swapFromNum = 0;
-
int swapToNum = 0;
-
int swapFromArrIndex = 0;
-
int swapToArrIndex = 0;
-
bool swapFromNumFound = false, swapToNumFound = false;
-
-
printf("Enter value , 0 to stop: \n");
-
while(1)
-
{
-
scanf("%d", &num[i]); // read value
-
if(num[i]==0)
-
break; // if 0 finished
-
i++;
-
}
-
-
printf("List now: ");
-
-
for(i=0;i<max;i++)
-
{
-
printf("%d ",num[i]);
-
}
-
printf("\n");
-
-
-
printf("Value to change:");
-
scanf("%d",&swapFromNum);
and some more....
2 1589
Suppose the user enters 3 values, then 0. (35, 20, 14, 0). Then there are only 4 values in the array - the 5th element is garbage, correct?
But when you display the list, you loop i from 0 to max (which is 5), printing every element of the array, garbage or not. You should keep track of how many values the user entered before the 0.
Suppose the user enters 3 values, then 0. (35, 20, 14, 0). Then there are only 4 values in the array - the 5th element is garbage, correct?
But when you display the list, you loop i from 0 to max (which is 5), printing every element of the array, garbage or not. You should keep track of how many values the user entered before the 0.
And you might notice that if you enter 10 numbers, strange things may happen after 5 and you may crash your programme.
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Daniel Ng |
last post by:
Thanks for the 2 guys how hep to to solve to count the number of recount
in forum. I'm done wif that. Got 1 more question..
Example
Record 1: ID 2 VALUE 2
Record 2: ID 3 VALUE 3
Record 3: ID 2...
|
by: Edwinah63 |
last post by:
Hi Everyone,
All the very best for 2004!!
i need urgent help with this problem, the users are about to skin me
alive!!
we have an access front end with linked to sql server 2k tables.
...
|
by: Amit D.Shinde |
last post by:
Hi Experts,
i am writting a stored procedure in sql server 7. Its a simple stored
procedure
It is my first stored procedure.
I want insert a record in table if the primary key field user id...
|
by: Amratash |
last post by:
Hi,
I needyour help.Its urgent.
My main aim is to log the site activities in a database at runtime.
I'm using W3C Extended Log File format for logging information. I can use
ODBC Logging directly...
|
by: blazerguns |
last post by:
Hi all,
I have used WMI method to set ipaddress in my system. But the
problem is that if i have multiple iface cards this program is setting
all the nic to same ip. I dont know how to stop this...
|
by: davidw |
last post by:
I have xml tree like
<root>
<field name="l1">
<field name="l11">
<field name="l111"/>
<field name="l112"/>
</field>
<field name="l2" stop="true">
<field name="l21">
|
by: Honey |
last post by:
the following is the programming code,..i need to stop the rebound animation on a click of the mouse and continue its animation on another click
so far I could stop the animation by using...
|
by: Teja |
last post by:
HI all,
I have a problem in accesing COM objects in threads. To be precise,
lets assume that I have a class GenericFunctions which is defined as
follows:
import win32com.client, pythoncom,...
|
by: kommaraju |
last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2
using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a
error message of HTTP STATUS 500
my jdbc...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.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: 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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |