| | |
1-D array
![]() |
•
•
Join Date: Apr 2006
Posts: 5
Reputation:
Solved Threads: 0
Write a C-Program that swaps the elements of 1-D array (10 elements) :
Example:
If the given array is:
5 8 9 2 3 1 11 17 43 6
The new array will be:
6 43 17 11 1 3 2 9 8 5
Your program should consist of the following functions:
1. Function READ to read the elements of the array
2. Function Display to print the elements of the new array
3. Function SWAP to swap the elements of the array
4. main function to call the previous functions
Note: in swap function, do not use an intermediate array for swapping, i.e. do not
declare a new array and fill it with the elements of the original array starting from the
last element. The swap operation should be done on one array
The size of array must be N (Entered by user)
my question is how can i swap the elements of the array????
Example:
If the given array is:
5 8 9 2 3 1 11 17 43 6
The new array will be:
6 43 17 11 1 3 2 9 8 5
Your program should consist of the following functions:
1. Function READ to read the elements of the array
2. Function Display to print the elements of the new array
3. Function SWAP to swap the elements of the array
4. main function to call the previous functions
Note: in swap function, do not use an intermediate array for swapping, i.e. do not
declare a new array and fill it with the elements of the original array starting from the
last element. The swap operation should be done on one array
The size of array must be N (Entered by user)
my question is how can i swap the elements of the array????
hi
looking at ur requierment, u can write in swap funtion like
hope this should work for u.
looking at ur requierment, u can write in swap funtion like
C Syntax (Toggle Plain Text)
for(int i=0; i<=4; ++i) { arr[i] = arr[i] + arr[9-i] ; arr[9-i] = arr[i] - arr[9-i] ; arr[i] = arr[i] - arr[9-i] }
•
•
Join Date: Apr 2006
Posts: 5
Reputation:
Solved Threads: 0
i solved it but the problem is that the first element of new array is always 0
C Syntax (Toggle Plain Text)
#include<stdio.h> main( ) { int i,n,j ; float arr[5], arr_new[5] ; int temp; for (i=0 ; i<5 ; ++i) { printf ("Enter the %dth element of array :", i) ; scanf ( "%f", &arr[i] ) ; } for (i=0 ; i<5 ; ++i) { printf ("the value of element #%d is %f \n", i,arr[i]) ; } for( i = 0, j = 5; i<5; i++,j--) { temp = arr[i]; arr[i] = arr_new[j]; arr_new[j] = temp; } for (i=0 ; i<5 ; ++i) { printf ("the new array value of element #%d is %f \n", i,arr_new[i]) ; } }
•
•
Join Date: Apr 2006
Posts: 5
Reputation:
Solved Threads: 0
guys i really got confused when u tryed yo solve iy with functions
C Syntax (Toggle Plain Text)
#include<stdio.h> void read(float arr[],int n) { int i ; for (i=0 ; i<n ; ++i) { printf ("Enter the %dth element of array :", i) ; scanf ( "%f", &arr[i] ) ; } void swp(float arr[],float arr_new[],int n) { int i,j, temp; for( i = 0, j = n-1; i<n; i++,j--) { temp = arr[i]; arr[i] = arr_new[j]; arr_new[j] = temp; } } void display(float arr_new[]); { int i; for (i=0 ; i<n ; ++i) { printf ("the new array value of element #%d is %f \n", i,arr_new[i]) ; } } void main() { float a[],b[], c[]; int m; printf ("Enter the size of array :") ; scanf("%d",&m); read(a,m); swap(a,b,m); display(c); }
![]() |
Similar Threads
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: compile error
- Next Thread: Menu repeats itself
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o ide inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






