Error in selection sort with function in C - c

I am trying to do a selection sort using function called min().
This is my code:
#include <stdio.h>
#include <conio.h>
void main() {
int i, temp, arr[20], n, loc;
int min(int [], int, int);
printf("Enter a range of the array");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter elements");
scanf("%d", arr[i]);
}
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
min(int arr[], int i, int n) {
int j, loc, temp;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = j;
}
}
return (temp);
}
getch();
}
the compiler is giving one error when compiling.
it saying:
Error SELECTIONSORT.C 22: Expression Syntax.
my line number 22 is min(int arr[],int i, int n) according to my compiler Turbo C++.
Please guide me where I am going wrong.
Thanks for any help.

There are multiple problems in your code:
The function min must be defined outside the body of the main() function.
Note that it is considered bad style to declare function prototypes in a local scope. Either define the function before the main() function or put the prototype before the main() function.
Also the prototype for main() without arguments should be int main(void).
In function min, you must initialize temp to i, or use i directly.
You should print the array contents after the sort, otherwise the program has no effect.
Here is a corrected version:
#include <stdio.h>
#include <conio.h>
int min(int [], int, int);
int main(void) {
int i, temp, arr[20], n, loc;
printf("Enter a range of the array: ");
if (scanf("%d", &n) == 1) {
for (i = 0; i < n && i < 20; i++) {
printf("Enter element %d: ", i);
if (scanf("%d", &arr[i]) != 1)
break;
}
n = i; // n is the actual number of inputs
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
for (i = 0; i < n; i++) {
printf("%d\n" array[i]);
}
}
getch();
return 0;
}
int min(int arr[], int i, int n) {
int j;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
i = j;
}
}
return i;
}

Related

Used function to find the sum of the array elements, I am getting garbage value

I wrote a code for finding the sum of array elements using functions. I have written like this(mentioned below). I am getting a garbage value as output.
#include<stdio.h>
int fact(int n, int i) {
int sum = 0, arr[100];
for (i = 0; i < n; i++) {
sum = sum + arr[i];
}
return sum;
}
int main() {
int n, arr[100], i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = fact(n, i);
printf("%d", sum);
return 0;
}
Pass the array arr to your function instead of re-declaring it inside fact
PS: Change the name of your function to something meaningful like arrSum.
Code:
#include<stdio.h>
int arrSum (int arr[], int n) {
int i, sum = 0;
for (i = 0; i < n; i++) {
sum = sum + arr[i];
}
return sum;
}
int main() {
int n, arr[100], i;
scanf("%d", &n);
if(n < 0 || n > 100)
return -1;
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = arrSum(arr, n);
printf("%d ", sum);
return 0;
}

Getting error: expression must have pointer-to-object type

I am getting the error in my last line of code (on the j variable). What could possibly be the issue? I would appreciate any help. Thank you in advance!
#include <stdio.h>
#include <stdlib.h>
void function(int *arr, int m, int n);
int main()
{
int array[4][2], i, j;
function((int*)array, 4, 2);
for (i = 0; i < 4; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", array[i][j]);
if (j == 1)
printf("\n");
}
}
system("pause");
return 0;
}
void function(int *arr, int m, int n)
{
int i, j, element;
for (i = 0; i < 4; i++) {
for (j = 0; j < 2; j++) {
printf("Give element: ");
scanf_s("%d", &element);
arr[i][j] = element;
}
}
}

Linear Search function compiles but no output

I do not understand what is wrong with this code of Linear search. It compiles but on execution exits without output.
turns - no. of test cases.
size - size of array.
x - element to be searched.
#include <stdio.h>
int linearSearch(int arr[], int size, int element)
{
int i = 0;
for(i=0; i< size; i++)
{
if(arr[i] == element)
{
return i;
}
}
return 0;
}
int main()
{
int turns, size;
scanf("%d", &turns);
while(turns--)
{
scanf("%d", &size);
int arr[size];
for(int j=0; j < size; j++)
{
scanf("%d", arr[j]);
}
int x;
scanf("%d", &x);
int k = linearSearch(arr, size, x);
}
return 0;
}
There is one major problem in your code.
First you need to pass address of your array element(&arr[j]).
And the output is not displaying because you are not printing it out.
The correct code is
#include <stdio.h>
int linearSearch(int arr[], int size, int element)
{
int i = 0;
for(i=0; i< size; i++)
{
if(arr[i] == element)
{
return i;
}
}
return 0;
}
int main()
{
int turns, size;
scanf("%d", &turns);
while(turns--)
{
scanf("%d", &size);
int arr[size];
for(int j=0; j < size; j++)
{
scanf("%d", &arr[j]);
}
int x;
scanf("%d", &x);
int k = linearSearch(arr, size, x);
printf("%d\n", k);
}
return 0;
}

Bubble sort does not seem to work-segmentation error

Hi there somehow my bubble sort that is suppose to work does not seem to work.Im not sure where the error is being caused.It is suppose to give me a sorted output list.It is giving me segmentation error and have been trying this for about 1 hr.Heres the code:
#include <stdio.h>
#include <stdlib.h>
#define N 10
void sort(int [], int);
void show(char *, int [], int);
int main(void)
{
int i, j, a[N];
srand(0);
for (j = 1; j <= 5; j++) {
// initialise array (pseudo-randomly)
for (i = 0; i < N; i++) {
a[i] = rand()%100;
}
// display, sort, then re-display
printf("Test #%d\n",j);
show("Sorting", a, N);
sort(a, N);
show("Sorted ", a, N);
}
return 0;
}
// sort array using bubble sort
void sort(int a[], int n)
{
int i, j, nswaps;
for (i = 0; i < n; i--) {
nswaps = 0;
for (j = n-1; j > i; j--) {
if (a[j] < a[j-1]) {
int tmp;
tmp = a[j];
a[j] = a[j-1];
a[j-1] = tmp;
nswaps++;
}
}
if (nswaps == 1) break;
}
}
// display array, preceded by label
void show(char *label, int a[], int n)
{
int i;
printf("%s:", label);
for (i = 0; i < n; i++) {
printf(" %02d", a[i]);
}
printf("\n");
}
Your are not technically sorting here.Instead of this for (i = 0; i < n; i--), try for (i = 0; i < n; i++).This is because you are starting with i=0 so in a FOR loop you need to increment i.

C Program - pointer array multiplication

This program is supposed to take 2 arrays and perform the dot product on each of the elements in the array.
My program is fine if the index of n is less than 5; however, once the index of the array is greater than 5 only the first element in the first array is wrong ( I checked by adding a printf statement in the function). I don't know how to fix this bug.
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
int v1[n];
int v2[n];
int v3[n];
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
printf("Enter numbers for the first array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
Correct code
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
int v1[n],v2[n],v3[n]; //you didn't initialize n
printf("Enter numbers for the first array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d ", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}

Resources