#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNING
#endif
#include <stdio.h>
#include <stdlib.h>
void bubbleSort(int matrix[9], int n);
int main(void) {
int matrix[9];
int n = 9;
printf("enter 10 values to the matirx \n");
for (int i = 0; i < 10; i++) {
printf("now %dth componet\n ", i+1);
scanf("%d", &matrix[i]);
}
bubbleSort(matrix[9], n);
return 0;
}
void bubbleSort(int matrix [9], int n) {
//bubble sort the given matrix
int temp = 0;
for (int i=n-1; i > 0; i--) {
// compare two values at j and j+1 and move left when j+1 is smaller than j
for (int j = 0; j < i; j++) {
if (matrix[j] > matrix[j + 1]) {
temp = matrix[j];
matrix[j] = matrix[j + 1];
matrix[j + 1] = temp;
}
}
printf("Check the matrix \n");
for (int i = 0; i < 9; i++) {
printf("%d ", matrix[i]);
}
printf("\n");
}
}
Hi, I am getting a read access violation error at
if (matrix[j] > matrix[j + 1]) {
**temp = matrix[j];**
matrix[j] = matrix[j + 1];
matrix[j + 1] = temp;
}
This part of the code. The code builds right, but when I run the program I get an error. Can anyone help me out to figure out the issue? I searched up a little bit and based on that I assume it has something to do with the pointer but I do not know why there would be an issue with a pointer since I never used it in my code.
For starters you declared an array that contains only 9 elements.
int matrix[9];
On the other hand, you are trying to enter 10 elements.
printf("enter 10 values to the matirx \n");
for (int i = 0; i < 10; i++) {
printf("now %dth componet\n ", i+1);
scanf("%d", &matrix[i]);
}
So the program already has undefined behavior.
In this function declaration
void bubbleSort(int matrix[9], int n);
the magic number 9 used in the declaration of the first parameter does not make a great sense. Just write
void bubbleSort(int matrix[], int n);
or
void bubbleSort(int *matrix, int n);
In this call
bubbleSort(matrix[9], n);
instead of passing the array matrix like
bubbleSort(matrix, n);
you are passing the non-existent element of the array matrix[9]. The compiler should issue a message that you are trying to convert an integer to a pointer.
Within the function you are using the magic number 9 instead of the parameter n in this loop
for (int i = 0; i < 9; i++) {
Pay attention to that it looks strange when a one-dimensional array is named matrix.
Using your approach the program can look the following way.
#include <stdio.h>
void bubbleSort( int matrix[], size_t n )
{
if ( n )
{
for ( size_t i = n - 1; i != 0; i-- )
{
// compare two values at j and j+1 and move left when j+1 is smaller than j
for ( size_t j = 0; j < i; j++ )
{
if ( matrix[j + 1] < matrix[j] )
{
int temp = matrix[j];
matrix[j] = matrix[j + 1];
matrix[j + 1] = temp;
}
}
}
}
}
int main(void)
{
enum { N = 9 };
int matrix[N];
printf( "enter %d values to the matirx \n", N );
for ( int i = 0; i < N; i++ )
{
printf( "now %dth componet\n", i+1 );
scanf( "%d", matrix + i );
}
bubbleSort( matrix, N );
for ( int i = 0; i < N; i++ )
{
printf( "%d", matrix[i] );
}
putchar( '\n' );
return 0;
}
The program output might look
enter 9 values to the matirx
now 1th componet
9
now 2th componet
8
now 3th componet
7
now 4th componet
6
now 5th componet
5
now 6th componet
4
now 7th componet
3
now 8th componet
2
now 9th componet
1
1 2 3 4 5 6 7 8 9
bubbleSort(matrix[9], n);
This passes just the final element of the list (coerced into an address) rather than the actual address of the list, which is what you probably intended.
That won't end well :-)
You should just pass in matrix.
A decent compiler should warn you of this, such as with gcc:
prog.c: In function ‘main’:
prog.c:21:22: warning: passing argument 1 of ‘bubbleSort’
makes pointer from integer without a cast
[-Wint-conversion]
21 | bubbleSort(matrix[9], n);
| ~~~~~~^~~
| |
| int
prog.c:7:21: note: expected ‘int *’ but argument is of
type ‘int’
7 | void bubbleSort(int matrix[9], int n);
| ~~~~^~~~~~~~~
Related
I'm having a bit of trouble with this problem. The full text of the problem is as follows : "Write a function that returns a version of the given array of non-negative integers where each zero value in the
array is replaced by the smallest odd value to the right of the zero in the array. If there is no odd value to the right of the zero,
leave the zero as a zero."
Here is my code:
#include <stdio.h>
void lowestOdd(int num[], int size) {
int i, temp;
for (i = 0; i < size; i++) {
if (num[i] % 2 != 0 && num[i] < num[i + 1]) {
temp = num[i];
}
}
for (i = 0; i < size; i++) {
if (num[i] = 0) {
num[i] = temp;
}
}
}
void printArray(int array[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d/n", array[i]);
}
}
int main() {
int i, size;
int myarr[20];
printf("What is the size of your array? \n");
scanf("%d", &size);
for (i = 0; i < size; i++) {
scanf("%d", &myarr[i]);
}
lowestOdd(myarr[20], size);
printArray(myarr[20], size);
return 0;
}
I've tried implementing pointers in the lowestOdd function, but to no avail. I do think they're necessary here, but I'm not really that good at pointers. The warnings I get are mostly 'warning: passing argument 1 of 'lowestOdd' makes pointer from integer without a cast [-Wint-conversion]'. Also, in my code, I haven't added the statements that would check whether the number is a zero or whether there are any odd values to the right of the zero.
In the declaration
int myarr[20];
myarr is the identifier - the name used to refer to the array itself. myarr has the type int [20].
When used in this expression
lowestOdd(myarr[20], size);
[20] is the array subscript operator, accessing index 20. This is index is out of bounds, as the valid indices for the type int [20] are 0 to 19. This out of bounds access will cause Undefined Behaviour.
This warning
warning: passing argument 1 of 'lowestOdd' makes pointer from integer without a cast [-Wint-conversion]
is given because, although an invalid index to access, the expression myarr[20] evaluates to an int. lowestOdd expects an int * as its first argument.
Similar to before, in
if (num[i] % 2 != 0 && num[i] < num[i + 1])
num[i + 1] will access num[size] when i is size - 1 (again, valid indices are 0 to size - 1).
This is assignment
if (num[i] = 0)
where you want a comparison
if (num[i] == 0)
Note that
scanf("%d", &size);
for (i = 0; i < size; i++) {
scanf("%d", &myarr[i]);
risks the same out of bounds access if the user enters a value greater than 20 for size.
Ignoring the out of bounds access for a moment, lowestOdd attempts to find the last occurrence of the smaller number from each pair of numbers in the array, where the left number must be odd.
It then replaces all zeroes in the array with this value.
There is a chance temp is never assigned anything, and thus has an indeterminate value.
This is not correct.
Here is an example program (using a variable-length array).
Note that the syntax array + i is equivalent to &array[i].
? : is the conditional operator: in a ? b : c, if a is non-zero the expression evaluates to b, else it evaluates to c.
#include <stdio.h>
int min(int a, int b)
{
return a < b ? a : b;
}
int find_lowest_odd(int *base, size_t len)
{
int value = 0;
for (size_t i = 0; i < len; i++)
if (base[i] & 1) /* base[i] % 2 != 0 */
value = value ? min(value, base[i]) : base[i];
return value;
}
void mutate_array(int *a, size_t len)
{
for (size_t i = 0; i < len; i++)
if (0 == a[i]) /* search from the next position */
a[i] = find_lowest_odd(a + i + 1, len - i - 1);
}
void print_array(int *a, size_t len)
{
for (size_t i = 0; i < len; i++)
printf("%d ", a[i]);
putchar('\n');
}
int main(void) {
size_t size;
printf("What is the size of your array?: ");
if (1 != scanf("%zu", &size))
return 1;
int array[size];
for (size_t i = 0; i < size; i++) {
printf("#%zu: ", i + 1);
if (1 != scanf("%d", array + i))
return 1;
}
print_array(array, size);
mutate_array(array, size);
print_array(array, size);
}
I/O:
What is the size of your array?: 10
#1: 0
#2: 2
#3: 0
#4: 5
#5: 3
#6: 0
#7: 7
#8: 2
#9: 0
#10: 0
0 2 0 5 3 0 7 2 0 0
3 2 3 5 3 7 7 2 0 0
Once you can try this.
Here's my naive approach. For every zero in the array it is checking for the smallest odd element from that position to the last index and storing it in variable named smaller. After checking it replaces the original value of that index with smaller one.
#include<stdio.h>
void lowestOdd(int *num, int size){
int i, j;
for(i = 0; i < size - 1; i++){
if (num[i] != 0) continue;
int smaller = 99998;
for(j = i+1; j < size; j++){
if (num[j] % 2 != 0 && num[j] < smaller) smaller = num[j];
}
if (smaller != 99998) num[i] = smaller;
}
}
void printArray(int *array, int size){
int i;
for (i=0; i<size; i++){
printf("%d\n", array[i]);
}
}
int main()
{
int i, size;
int myarr[20];
printf("What is the size of your array? \n");
scanf("%d", &size);
for (i=0; i<size; i++){
scanf("%d", &myarr[i]);
}
lowestOdd(myarr, size);
printArray(myarr, size);
return 0;
}
I'm a beginner in programing so please be understanding with my code.. I'm working on a problem set where I have to implement a selection sort using recursion. I feel like it should work but I get an error message and i can't figure out why.
the problem set consists in that I use a recursive function where i have to look for the largest number in an array, store it in the last position and sort the entire array using this method.
int array[n];
printf ("enter numbers: ");
for (i = 0; i < n; i++)
{
scanf ("%i", &array[i]);
}
selection_sort(n, array);
printf ("sorted numbers: ");
for (i = 0; i < n; i++)
{
printf ("%i", array[i]);
}
return 0;
here is the recursive function that i'd like to implement.
i used curpos to store the position of the largest number,
lastpos to store the location of the last element in the array,
and a tmp variable to store the largest number.
and this is the error message that i get.
.c:67:34: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'int *'; take the address with & [-Werror,-Wint-conversion]
return selection_sort(n, array[n-1]);
^~~~~~~~~~
&
if (n <= 0)
{
return 1;
}
else
{
for (i = 0; i < n; i++)
{
if (tmp <= array[i]) //look for the largest number and update it into tmp
{
tmp = array[i];
curpos = array[i]; //remember the location of the current largestnumber
}
}
lastpos = array[n-1]; // save the last element into a variable before swap
array[n-1] = tmp; // put the largest number into the last element
curpos = lastpos; // put the last element before swap into the changed location.
return selection_sort(n, array[n-1]);
}
}
I hope you can give me a hand to understand the recursion better. thank you so much in advance.
The return value is a red herring. You don't use it so your function may as well be void.
if (n <= 0)
{
return 1;
}
The second thing to notice is that you can't pass an array to a function. You are passing a pointer to the beginning of the array. This is good, because otherwise you wouldn't be able to sort it.
else
{
for (i = 0; i < n; i++)
{
Right here is an issue. You haven't initialised tmp and curpos. You need to do that before the loop.
if (tmp <= array[i]) //look for the largest number and update it into tmp
{
tmp = array[i];
curpos = array[i]; //remember the location of the current largestnumber
}
}
lastpos = array[n-1]; // save the last element into a variable before swap
array[n-1] = tmp; // put the largest number into the last element
curpos = lastpos; // put the last element before swap into the changed location.
Finally, right here you have the right idea, but the wrong notation. You want to pass the same array, but 1 less element
return selection_sort(n, array[n-1]);
Should be:
return selection_sort(n-1, array);
}
The error message means that in this call
return selection_sort(n, array[n-1]);
you are passing an element of the array of the type int with the index n-1. But the function expects a pointer of the type int *.
Moreover the value of the first parameter is always the same and equal to n.
Also the return type of the function does not make a sense. The function should be declared with the return type void.
Also you need to swap two elements if within the array there is found an element that is greater than the last element.
The function can be declared and defined the following way
void selection_sort( int a[], size_t n )
{
if (!( n < 2 ))
{
size_t i = --n;
for (size_t j = n; j-- != 0; )
{
if (a[i] < a[j]) i = j;
}
if (i != n)
{
int tmp = a[i];
a[i] = a[n];
a[n] = tmp;
}
selection_sort( a, n );
}
}
Here is a demonstration program.
#include <stdio.h>
void selection_sort( int a[], size_t n )
{
if (!( n < 2 ))
{
size_t i = --n;
for (size_t j = n; j-- != 0; )
{
if (a[i] < a[j]) i = j;
}
if (i != n)
{
int tmp = a[i];
a[i] = a[n];
a[n] = tmp;
}
selection_sort( a, n );
}
}
int main( void )
{
int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
const size_t N = sizeof( a ) / sizeof( *a );
for (size_t i = 0; i < N; i++)
{
printf( "%d ", a[i] );
}
putchar( '\n' );
selection_sort( a, N );
for (size_t i = 0; i < N; i++)
{
printf( "%d ", a[i] );
}
putchar( '\n' );
}
The program output is
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
May you help me with this exercise please?
Write a C program that reads 6 integers from the keyboard and assigns the first 5 values at the first 5 positions of an array; store the sixth value in a variable N. Write a function that, given input the array initialized with the first 5 values from the keyboard and the integer N, returns the array resized to contain 5 + N elements, such that each one of the new N elements corresponds to the sum of the numbers before it in the array.
In main, print the content of the array returned by the function.
It's OK also all in the main function.
I have the problem when I have to use the function realloc to increment the array from size = 5 to 5 + N.
This is my code:
int N, a, i;
int *ptr;
int arr[6];
for (i = 0; i < 5; i++) {
printf("Insert number in array, position(%d): ", i);
scanf("%d", &arr[i]);
}
N = arr[4];
a = 5 + N;
ptr = (int *)realloc(arr, sizeof(int) * a);
for (i = 4; i < a; i++) {
ptr + i = N * N; //<--- **problem!!**
}
for (i = 0; i < a; i++) {
printf("%d\n", ptr[i]);
}
free(ptr);
You cannot reallocate an array defined locally in a function nor defined globally. You can only call realloc on an object previously allocated with malloc(), calloc() or realloc() or with a NULL pointer. So you must allocate the initial array with 5 elements in main() and reallocate it in the function.
#include <stdio.h>
#include <stdlib.h>
int *extend_array(int *arr, int N) {
int a = 5 + N;
arr = realloc(arr, a * sizeof(int));
if (arr != NULL) {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
for (int i = 5; i < a; i++) {
arr[i] = sum;
sum += sum;
}
}
return arr;
}
int main() {
int N;
int *arr = malloc(5 * sizeof(int));
if (arr == NULL) {
printf("allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
printf("Insert number in array, position(%d): ", i);
if (scanf("%d", &arr[i]) != 1) {
printf("invalid input\n");
return 1;
}
}
printf("Insert the value of N: ");
if (scanf("%d", &N) != 1) {
printf("invalid input\n");
return 1;
}
int *ptr = extend_array(arr, N);
if (ptr == NULL) {
printf("reallocation failed\n");
} else {
arr = ptr;
for (int i = 0; i < 5 + N; i++) {
printf("%d\n", arr[i]);
}
}
free(arr);
return 0;
}
The assignment specifies that the function should take the array and the number N as arguments, but it would be better to make the initial size a variable and pass that to the function as well to make the code more generic, easier to extend, and less error prone as the constant 5 appears in many places.
If you need to reallocate an array then it initially must be allocated dynamically.
So this code
int arr[6];
//...
ptr = (int *)realloc(arr, sizeof(int) * a);
is invalid.
Pay attention to that according to the assignment you need to write a function that reallocates the array.
Bear in mind that it is a very bad style of programming to use "magic numbers" like 5. Instead use named constants or assign such numbers to variables and use them.
The program can look the following way.
#include <stdio.h>
#include <stdlib.h>
int * resize( int *a, size_t n, size_t m )
{
int *tmp = realloc( a, ( n + m ) * sizeof( int ) );
if ( tmp != NULL )
{
int sum = 0;
size_t i = 0;
while ( i < n ) sum += tmp[i++];
while ( i < n + m )
{
tmp[i] = sum;
sum += tmp[i++];
}
}
return tmp;
}
int main(void)
{
size_t n = 5;
int *a = malloc( n * sizeof( int ) );
size_t m = 0;
printf( "Enter %zu numbers. The last number shall be greater than 0: ", n + 1 );
for ( size_t i = 0; i < n; i++ )
{
scanf( "%d", a + i );
}
scanf( "%zu", &m );
int *tmp = resize( a, n, m );
if ( tmp != NULL )
{
a = tmp;
}
else
{
m = 0;
}
for ( size_t i = 0; i < n + m; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
free( a );
return 0;
}
The program output might look like
Enter 6 numbers. The last number shall be greater than 0: 1 2 3 4 5 6
1 2 3 4 5 15 30 60 120 240 480 960 1920 3840 7680
I am attempting to make a program that takes an array and reverts it backwards however the program must do this to the array in groups of three. So if the user enters the numbers 1, 2, 3, 4, 5, 6 into the array the program will then output: 3, 2, 1, 6, 5, 4.
When I run the current program I get: 3 2 1 4 5 6. If anyone could help me figure out why that would be great as I am a little confused.
Here is my code:
int * numbersProcessFour(int *x, int size)
{
int i = 0, three = 3, six = 6, nine = 9;
if (size < 4)
{
for (i; i < three; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
}else if (size > 3 && size < 7)
{
for (i; i < three; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
for (i; i < 6; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
}
else if (size > 6 && size < 10)
{
for (i; i < three; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
for (i; i < 6; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
for (i; i < 9; i++)
{
reverse_array(x, three);
printf("%d ", x[i]);
}
}
}
void reverse_array(int *x, int length)
{
int i, temp;
for (i = 0; i<length / 2; i++)
{
temp = x[i];
x[i] = x[length - i - 1];
x[length - i - 1] = temp;
}
}
Continuing from your comment to fluter's answer, you may be over thinking it a bit. In order to swap the 1st and 3rd element in each 3-element partition of an array, you simply need to step though the array 3-elements at a time. You need to decide how you will handle any final partial partition, but since your goal is to swap the 1st and 3rd, there is no 3rd in anything less than a full partition, so the logical choice is to ignore any final partial partition.
A variant of what you and fluter have done incorporating a swap would be:
/* reverse 1st and 3rd element in each group of 3 */
void rev3 (int *a, size_t sz)
{
if (sz < 3) return;
size_t i;
for (i = 0; i < sz; i += 3) {
if (sz - i < 3) break;
swap (&a[i], &a[i+2]);
}
}
You can put it together with:
#include <stdio.h>
void rev3 (int *a, size_t sz);
void swap (int *a, int *b);
int main (void) {
int a[] = {1,2,3,4,5,6,7,8,9};
size_t i;
rev3 (a, sizeof a/sizeof *a);
for (i = 0; i < sizeof a/sizeof *a; i++) printf (" %2d", a[i]);
putchar ('\n');
return 0;
}
void swap (int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
Example Use
When compiled and run it will give you the swap (reversal) of the 1st and 3rd elements throughout the array that you specify in your problem.
$ ./bin/revarr3
3 2 1 6 5 4 9 8 7
There is no difference whether you use a separate swap or whether you include that operation in your reversal function. There is also no need to incur the additional overheard of calling a recursive function when a procedural approach will work. Look over all the answers and compare/contrast the differing ways to accomplish your goal.
You have branches for each multiple of 3, that is inefficient. One way to solve it is you can take the array as a split by 3 smaller arrays, and reverse on them. Also, reversing an array of 3 elements is the same as swap the 1st and the 3rd element.
int i;
int temp;
for (i = 0; i < count; i += 3) {
if (i+2 >= count)
break;
temp = arr[i];
arr[i] = arr[i+2];
arr[i+2] = temp;
}
A generalized version of numberProcessFour might look like this.
int reverse_array_mod(int *input, size_t size, int mod)
{
int i, smod;
/* Error: return modulus if size cannot be divided by mod */
if(size%mod)
return size%mod;
smod = size/mod;
for(i=0; i<smod; i++)
reverse_array(input+i*mod, mod);
/* return 0 on success */
return 0;
}
Test
int main(int argc, char **argv)
{
int a[] = {0, 1, 2, 3, 4, 5};
int i, err, mod;
for(mod=1; mod<5; mod++) {
err = reverse_array_mod(a, 6, mod);
if(err) {
fprintf(stderr, "Error %d, modulus %d invalid\n", err, mod);
return err;
}
for(i=0; i<6; i++)
printf("%d\n", a[i]);
printf("\n");
}
return 0;
}
Result:
0
1
2
3
4
5
1
0
3
2
5
4
3
0
1
4
5
2
Error 2, modulus 4 invalid
try this
int *numbersProcessFour(int *x, int size) {
int i;
for(i = 0; i + 3 < size; i += 3){
reverse_array(x + i, 3);
}
if(size - i > 1)
reverse_array(x + i, size - i);
return x;
}
This is fairly simple problem to print the integer array in reverse order. Although whenever i try printing, it ends up displaying garbage value. Below is my program.
#include <stdio.h>
#include <conio.h>
int main()
{
int temp = { '\0' };
int num[9];
int i;
int j = 8;
printf("Enter 8 numbers\n");
for (i = 0; i < 8; i++)
{
scanf_s("%d", &num[i], 1);
}
for (i = 0; i <= j; i++, j--)
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
printf("\nThe numbers in reverse are\n");
for (i = 0; i <=8; i++)
{
printf("%d\n", num[i]);
}
_getch();
return 0;
}
Let just say i input numbers from 1 to 8, it does print the number in reverse but the first value it prints is a garbage value. I know i can use and If statement to counter the situation but is there a way to counter this problem without using if?
You've made two mistakes here:
With 8 numbers, the index of the highest item is 7, not 8. Set j to 7 on initialization to fix this.
When you iterate 8 numbers from index zero, use operator < or !=, not <= to avoid an off-by-one error. Your first loop does it right, but the last loop is broken.
In addition, you may want to reduce the size of the array to 8, because the ninth element is unused.
If you want to print the integers in your array in reverse, simply start at the last index, then work up to the top.
The third loop should look more like this:
int j = 7; // an array of size 8 starts at the 0th and ends at the 7th index.
while(j >= 0)
{
printf("%d", num[j]);
j--;
}
There are several logical inconsistences in your program,
You defined the array as having 9 elements
int num[9];
but enter only 8 elements
for (i = 0; i < 8; i++)
{
scanf_s("%d", &num[i], 1);
}
Thus the last element of the array with insex 8 is not initialized. Nevertheless in the loop that swaps elements of the array you access this uninitialized element
int j = 8;
//...
for (i = 0; i <= j; i++, j--)
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
There is also no need to use function scanf_s instead of scanf
Take into account that to output an array in the reverse order you need not to swap its elements.
The program that outputs an array in the reverse order without swapping its elements can look the following way
#include <stdio.h>
#include <conio.h>
#define N 9
int main( void )
{
int num[N];
int i;
printf( "Enter %d numbers\n", N );
i = 0;
while ( i < N && scanf( "%d", &num[i] ) == 1 ) i++;
printf( "\nThe numbers in reverse are\n" );
while ( i-- ) printf( "%d ", num[i] );
printf( "\n" );
_getch();
return 0;
}
If to enter a sequence of numbers
1 2 3 4 5 6 7 8 9
then the output will look like
9 8 7 6 5 4 3 2 1
If you want to swap elements of the array then the program can look like
#include <stdio.h>
#include <conio.h>
#define N 9
int main( void )
{
int num[N];
int n;
int i;
printf( "Enter %d numbers\n", N );
n = 0;
while ( n < N && scanf( "%d", &num[n] ) == 1 ) n++;
for ( i = 0; i < n / 2; i++ )
{
int tmp = num[i];
num[i] = num[n - i - 1];
num[n - i - 1] = tmp;
}
printf( "\nThe numbers in reverse are\n" );
for ( i = 0; i < n; i++ ) printf( "%d ", num[i] );
printf( "\n" );
_getch();
return 0;
}
If the input is the same as above then output will be
9 8 7 6 5 4 3 2 1
You can also keep the for loop the same and change the indexing
#define ARRAY_SIZE 8
// print in reverse order
for (i = 0; i < ARRAY_SIZE; i++)
{
printf("%d\n", num[ARRAY_SIZE - i - 1]);
}
I used a #define to make it easier to change the program when you need a different array size: just change at one place rather than the 5 you currently need.