Find max, min and sum of an array using function - c

I want to create a programm to find maximum, minimum and summation for a given array, but when I take my code to compiler, it returned expressed expression before 'int' and too few arguments to all the functions.
And here is my code, please tell me why it doesn't work.
#include <stdio.h>
#include <stdlib.h>
void print_array(int array[], int len) {
// print array on screen
int b;
printf("Array: ");
for (b = 0; b < len - 1; b++) {
printf("%d, ", array[b]);
}
if (b >= len - 1) {
printf("%d\n", array[b]);
}
}
// function
int min(int *x, int len) {
int mintemp = x[0], i;
for (i = 1;i < len;i++) {
if(x[i] < mintemp) {
mintemp = x[i];
}
}
return mintemp;
}
int max(int *y, int len) {
int maxtemp = y[0], j;
for (j = 1;j < len;j++) {
if(y[j] > maxtemp) {
maxtemp = y[j];
}
}
return maxtemp;
}
int sum(int *z, int len) {
int sumtemp = 0, k;
for (k = 0;k < len;k++) {
sumtemp = sumtemp + z[k];
}
return sumtemp;
}
int main() {
int array[] = {3, 9, 1, 2, 5, 8, 7, 6, 4, 10, 11};
int len = 11;
print_array(array, len);
// print other elements on screen
printf("Maximum: %d\n", max(int *x, int len));
printf("Minimum: %d\n", min(int *y, int len));
printf("Summation: %d\n", sum(int *z, int len));
return 0;
}

The problem was with calling the functions. Function call in C is like this
function_name(argument 1, argument 2, argument 3, ......, argument n);
There is no need to specifying the data type along the parameters in the call like you were doing in your code.
Also you were passing pointers x, y and z that do not point to your array.
I have corrected the code as shown below.
int main() {
int array[] = {3, 9, 1, 2, 5, 8, 7, 6, 4, 10, 11};
int len = 11;
print_array(array, len);
// print other elements on screen
printf("Maximum: %d\n", max(array, len)); // pass array and len
printf("Minimum: %d\n", min(array, len));
printf("Summation: %d\n", sum(array, len));
return 0;
}
If you want to pass the array to functions via pointers you can do it like this.
int main() {
int array[] = {3, 9, 1, 2, 5, 8, 7, 6, 4, 10, 11};
int len = 11;
int *x = array;
int *y = array;
int *z = array;
print_array(array, len);
// print other elements on screen
printf("Maximum: %d\n", max(x, len));
printf("Minimum: %d\n", min(y, len));
printf("Summation: %d\n", sum(z, len));
return 0;
}
Also there is no need to declare 3 different pointers and pass it to the functions, you can declare just one and reuse it with subsequent calls.
Hope this helps.!!

Related

Returning every s-th component of a vector abort problem in c

The task was to create a program in which you input a vector and it returnes every s-th component of the vector. For example, if x = (1, 2, 3, 4, 5, 6) and s = 2, the output is (1, 3, 5). But I get a zsh abort warning.
#include <stdio.h>
void sampleVector(double arr[], int n, int s){
int j = 0;
for (j=0; j<n; j++) {
arr[j] = 0;
printf("%d: ",j);
scanf("%lf",&arr[j]);
}
int i=1;
printf("%f,", arr[i]);
for (i=1; i<n; i++){
s=s*i;
printf("%f", arr[s]);
}
}
int main() {
int n;
scanf("%d", &n);
double arr[3]={0,0,0};
int s;
scanf("%d", &s);
sampleVector(arr, n, s);
}
This is my program so far!
void printfEveryNth(const int *array, size_t size, size_t n)
{
if(array && n)
{
for(size_t index = 0; index < size; index += n)
{
printf("%d ", array[index]);
}
printf("\n");
}
}
int main(void)
{
int array[] = {1, 2, 3, 4, 5, 6};
printfEveryNth(array, 6 , 2);
}
https://godbolt.org/z/e3Tsa8GKj

Remove negative numbers and sort an array

Good day everyone,
my task is to remove all negative numbers from an array, and shorten it (return the new length as the amount of positive numbers). I tried doing that by BubbleSort all negative number to the right, and new length would be (old length - number of swap). My code simply freezes up the system.
I would be grateful if you guys could help.
void swap(int *p, int *q) {
int h = *p;
*p = *q;
*q = h;
}
int remove_negatives(int *array, int length) {
int *a;
int n = length;
a = &array[n - 1];
for (int i = 0; i <= n - 1; i++) {
while (array < a) {
if (*array < 0) {
swap(a, array);
a--;
array++;
length--;
}
}
}
printialn(array, n);
return length;
};
int main(void) {
int a[] = {-1, 2, 4, -8, 3, 7, -8, 9, 3};
int l = sizeof(a) / sizeof(a[0]);
printiln(remove_negatives(a, l));
return 0;
}
The while loop never stops, that's probably the reason your code freezes.
Your code only changes the address when the if statement is true. Which the array in your main() will stuck on the second (a[1]) element. So if we change change the address when the if statement is false...
#include<stdio.h>
#include<stdlib.h>
void swap(int *p, int *q) {
int h = *p;
*p = *q;
*q = h;
}
int remove_negatives(int *array, int length) {
int *a, *head;
int n = length;
head = array;
a = &array[n - 1];
for (int i = 0; i <= n - 1; i++) {
while (array < a) {
if (*array >= 0) {
array++;
continue;
}
swap(a, array);
a--;
array++;
length--;
}
}
for (int i=0; i<length; i++) {
printf("%d ", *head);
head++;
}
puts("");
return length;
}
int main(void) {
int a[] = {-1, 2, 4, -8, 3, 7, -8, 9, 3};
int l = sizeof(a) / sizeof(a[0]);
remove_negatives(a, l);
return 0;
}
Now the while loop works, buts as #wovano said, the answer is still wrong. Your code isn't exactly a "bubble sort". you swap all the negative number to the end of the array and didn't actually sort the array.
So, let's start from the beginning.
To simplify the process, let bubble sort first, and then find the new array length.
#include<stdio.h>
#include<stdlib.h>
void swap(int *p, int *q) {
int h = *p;
*p = *q;
*q = h;
}
int bubble_sort(int *array, int length) {
int i, j;
// Bubble sort
for (i=0; i<length-1; i++) {
for (j=i+1; j<length; j++) {
if (array[i]<array[j]) swap(&array[i], &array[j]);
}
}
// Find new array length
for (i=length-1; i>=0; i--) {
if (array[i]>=0) break;
length--;
}
return length;
}
int main(void) {
int a[] = {-1, 2, 4, -8, 3, 7, -8, 9, 3};
int l = sizeof(a) / sizeof(a[0]);
l = bubble_sort(a, l);
for (int i=0; i<l; i++) printf("%d ", a[i]);
puts("");
return 0;
}

Is there a way to code something like this in C?

Input 1: 90009
Input 2: 8999
Input 3: 94738
(Output: 90009 94738 8999)
see the pattern
it sorts the value of 3 integer inputs in a such a way one input continues another by matching the starting digit and the ending digit of another integer
#include <stdio.h>
void swap( int *x , int * y)
{
double t = *x;
*x = *y;
*y = t;
}
int sort(int *x, int *y, int *z)
{
int getZ;
getZ = *z;
while(getZ>=10)
getZ=getZ/10;
if(*x % 10 == *y/ 1000)
swap(y,x);
if(*x%10 != *z/1000)
swap(z,y);
if(*y % 10 == getZ)
swap(z,y);
printf("Sorted Version is %d %d %d",*x,*y,*z);
}
For some reason im not getting the order that i want. Is there algorithm to write this code with?
The simplest way is just to check every permutation. This code can be more compactly written, but here's a very straightforward implementation:
#include <stdio.h>
int first_digit(int n) {
while (n > 10) n /= 10;
return n;
}
int last_digit(int n) {
return n % 10;
}
int sort(int *x, int *y, int *z) {
int n[3] = {*x, *y, *z};
int permutations[][3] = {
{0, 1, 2},
{0, 2, 1},
{1, 0, 2},
{1, 2, 0},
{2, 0, 1},
{2, 1, 0},
};
for (int i = 0; i < sizeof(permutations) / sizeof(*permutations); i++) {
int *idx = permutations[i];
if (last_digit(n[idx[0]]) == first_digit(n[idx[1]]) && last_digit(n[idx[1]]) == first_digit(n[idx[2]])) {
*x = n[idx[0]];
*y = n[idx[1]];
*z = n[idx[2]];
return 1;
}
}
return 0;
}
int main() {
int x=126, y=456, z=6001;
if (sort(&x, &y, &z)) {
printf("sorted version is %d %d %d\n", x, y, z);
} else {
printf("sort not possible for %d %d %d\n", x, y, z);
}
return 0;
}

Print elements after place in array in c

So I tried to print elements of array after some point or place
but its print me another number of garbage. The code need to print 7,8,9,5.
I'm sure that the problem is in the line :
for (arr=x+1; arr < arr+ n; arr++)
but I don't understand what to write instead this line.
Please help me and use * or & instead [](use pointers). thanks!
#include <stdio.h>
#include <stdlib.h>
void printAfterX(int* arr, int n, int* x);
int main(void)
{
int arr[] = { 4, 8, 6, 2, 1, 3, 5, 7, 8, 9, 5 };
printAfterX(arr, 11, arr + 6);
system("PAUSE");
return 0;
}
void printAfterX(int* arr, int n, int* x)
{
if (n >= arr)
{
printf("not found");
}
else
{
for (arr=x+1; arr < arr+ n; arr++)
{
printf("%d ",*arr);
}
}
}
Why do you need to mix pointers and indices? You are right, in the line for (arr=x+1; arr < arr+n; arr++) there is a problem, because arr < arr + n is always true for positive n. So, this loop will never end.
Also it is not clear what does if (n >= arr) mean. Here you compare number of elements in the array and pointer on this array. It is useless. It seems that you need to compare x and arr.
I'd recommend you to use index of element to simplify your code. Try this:
void printAfterX(int* arr, int n, int x)
{
if (x < 0 || x+1 >= n)
{
printf("not found");
}
else
{
for (int i = x+1; i < n; i++)
{
printf("%d ", arr[i]);
}
}
}
And to call this function you need to change third parameter:
printAfterX(arr, 11, 6);
Also it is better to avoid using of system("pause") - read here: system("pause"); - Why is it wrong?
Update:
Ok, if you need to use pointers, try this:
void printAfterX(int* arr, int n, int* x)
{
if (x < arr || x+1 > arr+n)
{
printf("not found");
}
else
{
for (x = x+1; x < arr + n; x++)
{
printf("%d ", *x);
}
}
}
It is obvious that arr < arr + n always holds true unless it has an overflow, so the loop won't stop before that happens.
Furthermore, by writing n >= arr, you compare an int to an int *, which definitely make no sense. To check the array bound, you should also check whether it's less than the lower bound.
Finally, I think it's better to use a simple while loop.
Here is the refined code:
#include <stdio.h>
#include <stdlib.h>
void printAfterX(int *arr, int n, int *x);
int main(void)
{
int arr[] = { 4, 8, 6, 2, 1, 3, 5, 7, 8, 9, 5 };
printAfterX(arr, 11, arr + 6);
getchar();
return 0;
}
void printAfterX(int *arr, int n, int *x)
{
if (x < arr || x >= arr + n)
{
printf("No such element");
}
while(x < arr + n)
{
printf("%d ", *x);
x++;
}
}
The line for (arr=x+1; arr < arr+ n; arr++) is an infinite loop as arr < arr+ n is always true.
Also in line if (n >= arr) you are comparing a int with a pointer,which is not meaningful.
Modify your function like this:
void printAfterX(int* arr, int n, int* x)
{
if (n <= x-arr) //checking it doesn't exceeds the size
{
printf("not found");
}
else
{
for (x++; x < arr+ n; x++)
{
printf("%d ",*x);
}
}
}
You have to pass the number of elements as a parameter to the function to know when you have reached the end of the array.
You also can't compare a variable of type int to varibale of type int *.Try this version:
#include <stdio.h>
#include <stdlib.h>
void printAfterX(int* arr, size_t size, size_t offset);
int main(void)
{
int arr[] = { 4, 8, 6, 2, 1, 3, 5, 7, 8, 9, 5 };
printAfterX(arr, 11, 7);
system("PAUSE");
return 0;
}
void printAfterX(int* arr, size_t size, size_t offset)
{
if (offset >= size)
{
printf("not found");
return;
}
else
{
while(offset < size)
{
printf("%d ", *(arr + offset));
offset++;
}
}
}
Try it:
#include <stdio.h>
#include <stdlib.h>
void printAfterX(int[], int, int);
int main(void)
{
int arr[] = { 4, 8, 6, 2, 1, 3, 5, 7, 8, 9, 5 };
printAfterX(arr, 11, 6);
system("PAUSE");
return 0;
}
void printAfterX(int arr[], int n, int x)
{
if (n <= arr[x])
{
printf("not found");
}
else
{
for (i=x+1; i < n; i++)
{
printf("%d ",arr[i]);
}
}
}

Completing my addarray () formula

I apologize for my vagueness in advance-this is my first post and I can really use some help.
The assignment is as follows:
/* Write a function named addarray() that returns the sum of the
elements of an array of int values. Your functions should take two
parameters, the array and the number of elements in the array. Make
your function work with the following program; */
/* arraysum.c
*
* Synopsis - displays the value returned by the function addarray()
* with 2 different sets of parameters.
*
* Objective - To provide a test program for the addarray() function.
* Your answers should be 55 and 0.
*
*/
#include <stdio.h>
int addarray(int [], int, int);
void main() {
int array1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int array2[4] = {0, 0, 0, 0};
printf("The sum of array1 = %d\n", addarray(array1, 0, 10));
printf("The sum of array2 = %d\n", addarray(array2, 0, 4));
}
This is my solution aid:
int addarray(int s[], int i, int n) {
int sum = 0;
for (i = 0; i < n; i++) {
sum += s[i];
}
return sum;
}
I cant seem to figure out how to get the proper result. Any help would be appreciated.
This is what I have completed so far:
#include <stdio.h>
int addarray(int array1[], int num_elements);
void print_array(int array1[], int num_elements);
void main(void)
{
int array1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum;
printf("\nArray:\n");
print_array(array1, 10);
sum = addarray(array1, 10);
printf("The sum is %d\n .", sum);
}
int addarray(int array1[], int num_elements)
{
int i, sum=0;
for (i=0; i<num_elements; i++)
{
sum = sum + array1[i];
}
return(sum);
}
void print_array(int array1[], int num_elements)
{
int i;
for(i=0; i<num_elements; i++)
{
printf("%d ", array1[i]);
}
printf("\n");
}
I cant figure out how to get a second array to be summed up.
Such as Array2.
int is a reserved word.you can't give a variable the name int.besides,the assignment says the function should take two parameters,not 3.check this :
#include <stdio.h>
int addarray(int arr[],int size);
void main() {
int array1[10] = {1,2,3,4,5,6,7,8,9,10};
int array2[4] = {0,0,0,0};
printf("The sum of array1 = %d\n", addarray(array1,10));
printf("The sum of array2 = %d\n", addarray(array2,4));
}
int addarray(int arr[],int size)
{
int sum = 0 , n ;
for( n = 0 ; n < size ; n++ )
{
sum += arr[n];
}
return sum;
}

Resources