473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

swap array

12 New Member
C programming

Below is my question and the code.

Write a program that will swap two values in an array. If the swap is successfull, the
program will display the updated list. If it contains any error, for example the values
are not in the list, the program will display appropriate error messages. Use related
functions to develop this program.
Example of outputs that the program must fulfill is as shown below (user input is in
bold font):

Example Output 1:
Key in value, 0 to stop: 6 8 11 35 64 0
list now : 6 8 11 35 64
value to change: 35
want to cahnge with: 8
list now: 6 35 11 8 64


Example Output 2:
Key in value, 0 to stop: 6 8 11 35 64 0
list now: 6 8 11 35 64
value to change: 35
want to cahnge with: 27
ERROR!! : no this number 27!
list now: 6 8 11 35 64

Example Output 3:
Key in value, 0 to stop: 0
list now: EMPTY LIST!



This is my code:


Expand|Select|Wrap|Line Numbers
  1.  #include <stdio.h> 
  2. #define max 5
  3.  
  4. int main()
  5. {
  6.     int num[max], i=0;
  7.     int swapFromNum = 0;
  8.     int swapToNum = 0;
  9.     int swapFromArrIndex = 0;
  10.     int swapToArrIndex = 0;
  11.     bool swapFromNumFound = false, swapToNumFound = false;
  12.  
  13.     printf("Enter value, 0 to stop: \n");
  14. while(1)
  15. {
  16. scanf("%d", &num ); // read value
  17. if(num[i]==0)
  18. break; // if 0 finished
  19. i++;
  20. }
  21.  
  22. printf("List now: ");
  23.  
  24.      for(i=0;i<max;i++)
  25.      {
  26.         printf("%d ",num[i]);
  27.      }
  28.      printf("\n");
  29.  
  30.  
  31.      printf("Value to change:");
  32.      scanf("%d",&swapFromNum);
  33.  
  34.      for(i=0;i<max;i++)
  35.      {
  36.         if(num[i] == swapFromNum)
  37.          {
  38.             swapFromNumFound = true;
  39.             swapFromArrIndex = i;
  40.             }
  41.         }
  42.  
  43.          if (swapFromNumFound == false)
  44.      { 
  45.          printf("ERROR!! : No this Value\n");
  46.          } 
  47.          return main();
  48.  
  49.  
  50.  
  51.      printf("Want change with value:");
  52.      scanf("%d",&swapToNum);
  53.  
  54.  
  55.         for(i=0;i<max;i++)
  56.      {
  57.         if(num[i] == swapToNum)
  58.          {
  59.             swapToNumFound = true;
  60.             swapToArrIndex = i;
  61.          }
  62.      }
  63.  
  64. if(swapToNumFound == false)
  65.      {
  66.         printf("ERROR!! : No this value\n");
  67.      }
  68.  
  69.  
  70.     if(swapFromNumFound && swapToNumFound)
  71.      {
  72.          num[swapFromArrIndex] = swapToNum;
  73.          num[swapToArrIndex] = swapFromNum;
  74.          printf("List now: ");
  75.          for(i=0;i<max;i++)
  76.          {
  77.          printf("%d ",num[i]);
  78.      }
  79.          printf("\n");
  80.      }
  81.  
  82. }
  83.  
my output is:

Enter value, 0 to stop:
1
2
0
List now: 1 2 0 4202500 37814140
value to change:


1. Why my output cannot stop after 0, it still auto generate the number which I didn't key in?
Mar 2 '07 #1
2 3021
horace1
1,510 Recognized Expert Top Contributor
you enter 3 numbers into num[] but you print 5 elements hence the last two values are rubbish
you need to set max to the number of elements read, e.g.
Expand|Select|Wrap|Line Numbers
  1. #define MAX 5
  2.  
  3. int main()
  4. {
  5.     int num[MAX], i=0;
  6.     int swapFromNum = 0;
  7.     int swapToNum = 0;
  8.     int swapFromArrIndex = 0;
  9.     int swapToArrIndex = 0;
  10.     bool swapFromNumFound = false, swapToNumFound = false;
  11.  
  12.     printf("Enter value, 0 to stop: \n");
  13. while(1)
  14. {
  15. scanf("%d", &num[i] ); // read value
  16. if(num[i]==0)
  17. break; // if 0 finished
  18. i++;
  19. }
  20. int max=i;   // note number of elements read
  21. printf("List now: %d", max);
  22.  
  23.      for(i=0;i<max;i++)
  24.      {
  25.         printf("%d\n ",num[i]);
  26.      }
  27.      printf("\n");
  28.  
  29.  
Mar 2 '07 #2
Ganon11
3,652 Recognized Expert Specialist
In your while loop, you should also make sure the user cannot enter more than 5 numbers. Your array only has room for 5 elements, but your code will let them enter numbers in any position, possibly rewriting a lot of important data.
Mar 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: JKop | last post by:
union SignedChoice{ long with_sign; unsigned long without_sign; }; int main() { SignedChoice data;
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
6
by: Jack White | last post by:
Does anyone know if an analogue to the "swap()" function found in C++ exists in C#. For those not familiar, let's say you have a function that loads some collection passed in by reference. If an...
8
by: silversnake | last post by:
hi all, I'm trying to swap two array location within a 2d array using the swap function. I know that how to swap with in a 1d array eg " swap(array, inner, inner+1); " but how do I use swap within a...
3
by: NK | last post by:
char *a="nnnnnnn fgjfjgjf"; char *b="ABC jjjjjjjjjhhhh"; to swap these two string...... this swap fn is working template<class T> void Swap( const T *&a,const T *&b)//no need to write...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
4
by: Robert LaMarca | last post by:
Hi, I am using numpy and wish to create very large arrays. My system is AMD 64 x 2 Ubuntu 8.04. Ubuntu should be 64 bit. I have 3gb RAM and a 15 GB swap drive. The command I have been trying...
0
by: Robert Kern | last post by:
Terry Reedy wrote: Presumably, he's using numpy.ones() as an example of creating a large array, not because he actually needs an array full of 1s. -- Robert Kern "I have come to believe...
21
by: raylopez99 | last post by:
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et al. (3rd edition) (highly recommended--it's packed with information, and is a desktop reference book) the following statement is...
0
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
jinu1996
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...
1
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
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...
0
agi2029
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,...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.