I have some work with this code and i need a swap function that can get a arrays like a[j].
How I need to transport to another function something like this?
#include <stdio.h>
void bubble_sort(int *a, int n) {
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) swap(&a[j], &a[j + 1]);
}
}
}
This is the code, so how I can call swap function with a[j]? Do I need to call function like this or?
int swap (int a[],int b[])
int swap (int *a,int *b)
With this second call i am sure that it will work Am i right ? But how can i call this function like the first example?
#define MAX 100
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
return 0;
}
void bubble_sort(int a[], int n) {
int i;
for(i=0;i<n;i++)
{
if(a[i]<a[i+1])
swap(a[i],a[i+1]);
}
return 0;
}
int main()
{
int a[4]={1,2,3,0};
int n=4;
bubble_sort(a,n);
for(int i=0;i<n;i++)
{
printf("%d",a[i]);
}
}
I used that code Segmentation fault (core dumped)
Your function needs to take 2 pointers like this:
void swap(int * const a, int * const b) {
int temp = *a;
*a = *b;
*b = temp;
}
int swap (int a[], int b[]); will also work since in function parameters int* a and int a[] are the same thing. But it is confusing - it implies that the pointer is pointing to an array, while you only want to swap 2 integers.
Here's how you could do it the other way:
void swap(int a[], int b[]) {
int temp = *a;
*a = *b;
*b = temp;
}
Related
I want to output the median of the array using pointer functions, here is my code:
#include <stdio.h>
void swap(int *a,int *b) {
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void BubbleSort(int n, int arr[]) //passing reference
{
int i, j;
for(i = 0; n - 1 > i; i++) // jika array sepanjang 8, proses bubblesort terjadi hanya 7 kali
{
for(j = 0; n - 1 > j; j++)
{
if(arr[j] > arr[j + 1])
{
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int* find_middle(int a[], int n){
int sum,i;
int median;
if(n%2!=0){
median = (n/2)+1;
}
else if(n%2==0){
median = (n+1)/2;
}
return &a[median];
}
int main(){
int naData[10]={0,1,2,3,4,5,6,7,8,9};
int naData2[11]={9,8,7,5,4,3,2,1};
BubbleSort(sizeof(naData),naData);
BubbleSort(sizeof(naData),naData);
int func1 = *find_middle(naData, sizeof(naData));
int func2 = *find_middle(naData2, sizeof(naData2));
printf("%d\n", func1);
printf("%d\n", func2);
return 0;
}
Currently, this function outputs
2
87
Here is what it should output:
5
5
How to fix this problem, while still implementing the pointers?
p.s. If the array has even elements, take the larger element as the median.
p.s. naData2 has the size [11], and it cannot be changed since it is a requirement from the professor
so i get two parameters, one is N [ how big the array would be] and nr_vals [ this is the range, if nr_vals == 2, then range would be ( 0~1)]. I'm getting an error in swap function EXC_BAD_ACCESS. and the permutations are not printing right. any ideas?
My Swap Function ;
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Main fun;
void perm_rec_1(int N, int nr_vals){
int array[N];
int tempAr = 0;
for (int arrayFiller = 0; arrayFiller <= N; arrayFiller++)
{
if (arrayFiller == nr_vals){
tempAr = 0;
}
array[arrayFiller] = tempAr;
tempAr++;
}
prem_rec_help(N, nr_vals, array);
}
Secondary;
void prem_rec_help(int N, int nr_vals, int array[])
{
int tempArray[N];
copy_array(array, tempArray, N);
show(tempArray, N);
int M = 0;
int solid = N;
last_helper(tempArray, array, M, N, solid, nr_vals);
}
Recursive Function;
void last_helper(int array[],int temp[], int M, int N, int soild, int nr_vals ){
if (M != N)
{
last_helper(array,temp, M+1, N, soild, nr_vals);
}
for ( int i = 0; i < nr_vals; i++)
{
temp[M] = i;
show(temp, soild);
}
M--;
if(M == 0)
{
for( int swaper = 0; swaper <= soild; swaper++)
{
swap(array[swaper], array[swaper+1]);
copy_array(array, temp, soild);
if(swaper == soild)
{
return;
}else{
last_helper(array,temp, M, N, soild, nr_vals);
}
}
}
}
I am trying to sort an array from least to greatest using pointers instead of array subscripts. I am not sure where the problem is but when i run this code, the values are returned in the same order that they were entered. The find_largest and swap functions both do exactly what they say. The selection_sort function uses a for loop to sort the numbers from right to left (greatest to smallest, right to left). I have been staring at this for a while now and it looks like it should work fine but like i said, for some reason the numbers are returned in the same order they were entered.
Here is my code:
#include <stdio.h>
#define N 5
void selection_sort(int *a, int n);
int *find_largest(int *a, int n);
void swap(int *p, int *q);
int main(void)
{
int i;
int a[N];
printf("Enter %d numbers to be sorted: ", N);
for (i = 0; i < N; i++)
scanf("%d", (a+i));
selection_sort(a, N);
printf("In sorted order:");
for (i = 0; i < N; i++)
printf(" %d", *(a+i));
printf("\n");
return 0;
}
void selection_sort(int *a, int n)
{
int i = 0;
int *largest;
for(i = 0; i < n; i++){
largest = find_largest(a, n-i);
swap(largest, a+(n-1-i));
}
}
int *find_largest(int *a, int n){
int *p = a;
int *largest = p;
for(p = a; p < a+n-1; p++){
if(*(p+1) > *p){
largest = (p + 1);
}
}
return largest;
}
void swap(int *p, int *q){
int *temp;
temp = p;
p = q;
q = temp;
}
There are two mistakes in your code.
One, logical in the find_largest function:
int *find_largest(int *a, int n){
int *p = a;
int *largest = p;
for(p = a; p < a+n-1; p++){
if(*(p+1) > *largest){ <---- //here you were checking for *(p)
largest = (p + 1);
}
}
return largest;
}
the other is with pointers in swap function:
void swap(int *p, int *q){
int temp;
temp = *p;
*p = *q;
*q = temp;
}
As John Bollinger mentioned in the comments, swap() does not function correctly - all it does is reassign pointers that quickly go out of scope.
Here is a rewrite of that function that does work. Just swap it in and it fits perfectly.
void swap(int *p, int *q){
int temp;
temp = *p;
*p = *q;
*q = temp;
}
Thanks to John Bollinger.
This function should return a pointer to the first occurrence of the largest int in an array without using the index operator. It can find and print the largest int, but how do I make it return a pointer? And how do I test if it was successful?
int *arr_first_max(const int *a, size_t n) {
const int *k;
int largest = 0;
for (k = a; *k != '\0'; k++) {
if (*k > largest) {
largest = *k;
}
}
printf("%d\n", largest);
return &largest;
}
Edit: size_t n is supposed to be used but I'm not sure how to include it.
And another answer assuming:
n is length of array a (in terms of elements, not bytes)
You want to iterate through the whole array a
Then code is:
const int *arr_first_max(const int *a, size_t n) {
const int *largest = a;
while(n--) {
if(*a>*largest)
largest = a;
a++;
}
printf("%d\n", *largest);
return largest;
}
In all functions n is assumed to be >0.
Just keep your logic, but instead of storing the max, store the index to the max, in order to return a pointer to the array at that index.
int *arr_first_max(const int *a, size_t n) {
int i,l;
for (l=i=0 ; i<n ; i++)
if (a[i] > a[l]) l = i;
return a+l;
}
Edit Pointer only version (based on comments) which does n-1 iterations
const int *arr_first_max(const int *a, size_t n) {
const int *most;
for (most=a++ ; --n ; a++) if (*a > *most) most = a;
return most;
}
And since I misread the question, a recursive bonus!
const int *arr_first_max_r(const int *most, const int *a, size_t n) {
if (*a > *most) most = a;
return --n ? arr_first_max_r(most, ++a, n) : most;
}
to be called like this
printf("Biggest int is : %d\n", *arr_first_max_r(a, a, n));
This works. You detect the largest number like you did then run the same loop again to see where the number is then return that address. I also changed some data types from int to const int and I removed the 2nd parameter because you're checking for null characters.
#include <stdio.h>
#include <stdlib.h>
const int *arr_first_max(const int *a) {
const int *k;
int largest=0;
for (k = a; *k != '\0'; k++) {
if (*k > largest){
largest = *k;
}
}
for (k = a; *k != '\0'; k++) {
if (*k == largest){
return k;
}
}
}
int main(){
int nums[6]={7,2,1,5,4,6};
const int* y=arr_first_max(nums);
printf("%d\n", *y);
}
Assuming
the function's signature is correct and complete (arr_first_max alludes that you search in an array for the first max element)
and thus int* a is the pointer to the first element (i.e. equivalent to int a[] in a signature); I'll leave the const specifier in this case, though I think you could omit/discard it if the exercise permits that
size_t n is the size of the array or, in other words, the number of elements in the array
you want to return a pointer to largest element in the array without using the subscript [] operator
then this would be a solution
int *arr_first_max(const int *a, size_t n) {
int *largest, *k;
for (largest = k = a; (k - a) < n; k++)
if (*k > *largest)
largest = k;
return largest;
}
That is because I quote from The C Programming Language:
Pointer subtraction is also valid: if p and q point to elements of
the same array, and p<q, then q-p+1 is the number of elements from
p to q inclusive.
i won't guess what size_t n is for
int *arr_first_max( int *a, size_t n) {
int *largest = a;
while(*a){
a++;
if(*a>*largest) largest=a;
}
return largest;
}
This program is supposed to take an array, and sort it from lowest to highest value. My program won't sort any values though. I believe the error is in the selectionSort. The values i and j are present in the function, I printed them out inside the function but they are not passed into the swap function. I tried making i and j pointers but it didn't work. I just have no clue on what to do next. Any help would be appreciated.
#include <stdio.h>
#define N 5
void selectionSort(int *a, int n);
int *findLargest(int *a, int n);
void swap(int *p, int *q);
int main(void)
{
int i;
int a[N];
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
selectionSort(a, N);
printf("In sorted order:");
for (i = 0; i < N; i++) {
printf(" %d", a[i]);
}
printf("\n");
return 0;
}
void selectionSort(int *a, int n)
{
int *p = a;
int i;
int j;
if (n == 1) {
return;
}
i = *(p+n-1);
j = *findLargest(a, n);
swap(&i, &j);
selectionSort(a, n - 1);
}
int *findLargest(int *a, int n)
{
int *p;
int *p_max = a;
for(p = a + 1; p < a + n - 1; p++) {
if ( *p > *p_max)
p_max = p;
}
return p_max;
}
void swap(int *p, int *q)
{
int temp = *(p-1);
*(p-1) = *q;
*q = temp;
}
The problem is in your call of swap: you swap the content of two local variables
int i;
int j;
... // Some other code, then
swap(&i, &j);
This has no effect on the original array. You should be passing p+n-1 and findLargest(a, n) directly, or store their results in pointers, not in ints:
swap(p+n-1, findLargest(a, n));
In addition, your swap is broken: rather than swapping the content of two pointers, it assumes that p points one element past the target location. This is a bad assumption to make in a general-purpose function, such as swap, and it also leads to undefined behavior in your program.
void swap(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}