How to print elements from another function into main function - arrays

I can't figure out how to print array elements from my function into the main program so if some can examine this code and help me fix it I would appreciate it. The program is supposed to take the length of the array from user input and then ask for its elements and print them out afterward.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int arrayN(int N) {
printf("Input array lenght: ");
scanf("%d",&N);
if(N>2) {
return N;
} else {
return 0;
}
}
int arrayelements(int array[], int array_length) {
int loop, i, N;
array_length = arrayN(N);
printf("Enter elements of the array: \n");
for(int i = 0; i < array_length; ++i) {
scanf("%d", &array[i]);
}
for(loop = 0; loop < array_length; loop++) {
printf("%d ", array[loop]);
}
}
int main() {
int N, array[], array_length;
int b = arrayelements(array[], array_length);
int a = arrayN(N);
printf("Array length is: %d \n", a);
printf("Elements of array are: %d \n", b);
return 0;
}

I reworked your example code. Hope it is what you want.
Focus lied on fixing the array declaration issues, memory allocation and
user input.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
int userinput_integer(const char *fmt, ...){
int N, rv = 0;
va_list va;
va_start(va, fmt);
vprintf(fmt, va);
while(1){
rv = scanf("%d", &N);
if (1 == rv) break;
printf("Input error! The input >>");
do{
rv = fgetc(stdin);
if (isprint(rv)) putchar(rv);
}while(rv != EOF && rv != '\n');
printf("<< is not a valid integer.\nPlease try again: ");
}
va_end(va);
return N;
}
int userinput_arraylength(void) {
int N;
N = userinput_integer("Input array lenght: ");
if(N>2) {
return N;
} else {
printf("Invalid length\n");
return 0;
}
}
int userinput_arrayelements(int *array, int N) {
printf("Enter elements of the array: \n");
for(int i = 0; i < N; ++i) {
array[i] = userinput_integer("%d: ", i);
}
return N;
}
void print_arrayelements(int *array, int N){
for(int i = 0; i < N; ++i) {
printf("%d ", array[i]);
}
}
int main() {
int N, *array;
N = userinput_arraylength();
array = malloc(N * sizeof(*array));
if (NULL == array){
printf("Allocation error!\n");
exit(-1);
}
N = userinput_arrayelements(array, N);
printf("Array length is: %d \n", N);
printf("Elements of array are:\n");
print_arrayelements(array, N);
free(array);
return 0;
}

Fistly, declaration of array is not correct.
It should be array[] = {0}
Secondly, you cannot call your array elements function before arrayN function, the size of array should be entered first
And in the array elements() there is no need to call the size function you can directly pass the size of array when calling the array elements ()
Here's the code:
#include <stdio.h>
#include<malloc.h>
int *getarray()
{
int size;
printf("Enter the size of the array : ");
scanf("%d",&size);
int *p= malloc(sizeof(size));
printf("\nEnter the elements in an array");
for(int i=0;i<size;i++)
{
scanf("%d",&p[i]);
}
return p;
}
int main()
{
int *ptr;
ptr=getarray();
int length=sizeof(*ptr);
printf("Elements that you have entered are : ");
for(int i=0;ptr[i]!='\0';i++)
{
printf("%d ", ptr[i]);
}
return 0;
}

Related

Printing array elements as difference between the max value of the array

I wrote a program to get an array which will return an array with elements as difference between max value and remaining elements.
#include <stdio.h>
void behind(int *, int);
int main(void) {
int array[10];
int N, i;
scanf("%d", &N);
for (i=0; i<N; i++) {
scanf("%d", &array[i]);
}
behind(array, N);
for (i=0; i<N; i++) {
printf("%d\n", array[i]);
}
return 0;
}
void behind(int *ptr,int size) /* Write your function behind() here: */
{
int i,max=0;
for(i=1;i<size;i++){
if(ptr[i-1]>=ptr[i])
max=ptr[i-1];
else
max = ptr[i];
}
for(i=0;i<size;i++);
ptr[i]=max-ptr[i];
}
why my program always return the same array that I got as input?

function for multiple occurrences in linear search using pointer

This is the code i wrote for multiple occurrences in linear search.Can you please help me point out the mistake ? I want the function to store multiple values in the pointer array and then later to print the array
#include <stdio.h>
void linearsearch(int n,int a[n],int x,int count,int *b[count])
{
count=0;
int i;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
count+=1;
}
}
int j=0;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
b[j]=i;
j++;
}
}
}
int main()
{
int n;
scanf("%d",&n);
int a[n];
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int x;
scanf("%d",&x);
int count;
int *b[count];
linearsearch(n,a,x,count,b);
for(i=0;i<count;i++)
{
printf("%d",*b[i]);
}
return 0;
}
I think there are several errors.
count must be initialized, like int count = 0;
count variable is not changed after linearsearch function.
b array should allocated dynamically.
suggested patch is:
#include <stdio.h>
void linearsearch(int n,int a[n],int x,int *count,int **b)
{
count=0;
int i;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
count+=1;
}
}
int j=0;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
*b = realloc(*b, sizeof(int) * (j + 1));
(*b)[j]=i;
j++;
}
}
}
int main()
{
int n;
scanf("%d",&n);
int a[n];
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int x;
scanf("%d",&x);
int count = 0;
int *b = NULL;
linearsearch(n,a,x,&count,&b);
for(i=0;i<count;i++)
{
printf("%d",b[i]);
}
return 0;
}
Well the ideal way would be to allocate the memory dynamically based of the number of x found. But well let's look at the errors first. Maybe after this discussion you can write the code.
b[j]=i;
In the called function let's dissect the type of this.
i is of type int. And b[j] is of type int*. Then you assigned them (type mismatched). Then %d expects an int but you passed something of type int*. This is undefined behavior.
Now there was another flaw you had - you passed count and somehow you expect the value you changed in search will be there in main(). C is pass by value and you changed to a local variable. That change is lost when the called function ends.
#include <stdio.h>
#include <stdlib.h>
int* linearsearch(int n,int* a,int x,int* count)
{
*count=0;
if( n <= 0){
fprintf(stderr, "%s\n","Error" );
exit(1);
}
int *t = malloc(sizeof(*t)*n);
if( t == NULL){
perror("Malloc failed");
exit(1);
}
for(int i = 0; i < n; i++)
if(a[i] == x)
t[(*count)++] = i;
int *temp = realloc(t,sizeof(*temp)* (*count));
if( temp == NULL){
perror("Realloc failed");
exit(1);
}
t = temp;
return t;
}
int main(void)
{
int n;
if( scanf("%d",&n)!= 1){
fprintf(stderr, "%s\n", "Error in input");
exit(1);
}
if( n <= 0 ){
fprintf(stderr, "%s\n", "Error in input : must be greater than 0");
exit(1);
}
int a[n];
for(int i=0; i < n; i++)
if(scanf("%d",&a[i])!=1){
fprintf(stderr, "%s\n","Error in input." );
exit(1);
}
int elmt_to_find;
if( scanf("%d",&elmt_to_find)!= 1){
fprintf(stderr, "%s\n", "Error in input : Element to find(must be integer)");
}
int count;
int *b = linearsearch(n,a,elmt_to_find,&count);
for(int i = 0; i < count; i++)
printf("%d ",b[i]);
printf("%s","\n");
free(b);
return 0;
}
If you want to stick with using VLA for b, you can alter your linearsearch to return count only if b is NULL. Then, you can create b as VLA, and pass it back to linearsearch again to be populated.
int count = linearsearch(n, a, x, 0, 0);
int b[count];
linearsearch(n, a, x, count, b);
Then, your function could look like:
int linearsearch(int n,int a[n],int x,int count,int *b[count])
{
int i;
if(count==0)
{
for(i=0;i<n;i++)
{
if(a[i]==x)
{
count+=1;
}
}
}
if (b==0)
{
return count;
}
int j=0;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
b[j]=i;
j++;
}
}
return count;
}

Create a dynamic array and assign random integer values to it?

int* create_array(char category, int n){
int *a;
a = malloc(n* sizeof(int));
for (int i = 0; i < n; i++) {
int x;
srand( time(NULL));
x = rand();
a[i] = x;
}
When I print this code, it just prints the same random variable 'n' times.
You can use srand(getpid() or you can specify ther range of random numbers using x=rand()%11 generates from 0-10;
You can try something like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *create_array(char category, int n);
int
main(int argc, char const *argv[]) {
time_t t;
int *array;
int i, n;
char category;
srand((unsigned)time(&t));
printf("Enter category: ");
if (scanf("%c", &category) != 1) {
printf("Invalid category.\n");
exit(EXIT_FAILURE);
}
printf("Enter n numbers: ");
if (scanf("%d", &n) != 1) {
printf("Invalid n value.\n");
exit(EXIT_FAILURE);
}
array = create_array(category, n);
printf("Your n random numbers between 0-10:\n");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
free(array);
return 0;
}
int
*create_array(char category, int n) {
int *array;
int i, candidate;
array = malloc(n * sizeof(*array));
for (i = 0; i < n; i++) {
candidate = rand() % 10;
array[i] = candidate;
}
return array;
}

Returning array made with calloc to main

So I have a problem:
My program has to find the most common element in an array. We have to make this array with calloc. My code is working but I am asked to move part of my code to a function in which I have to create an array and enter numbers.
int main() {
int n, i, dazn;
int *A;
dazn = 0;
printf("Iveskite naturalu skaiciu: \n");// enter how many elements in an array we will have
scanf("%d", &n);
A = (int*)calloc(n, sizeof(int)); // Starting from here...
for (i = 0; i < n; i++) {
printf("Iveskite skaiciu \n");
scanf("%d", &A[i]); //
}
... // and ending here have to be in a function
... // sorting and finding the most common element
As instructed, move the code to a separate function:
#include <stdio.h>
#include <stdlib.h>
int *allocate_and_read_array(int size) {
int *A = (int*)calloc(size, sizeof(int));
if (A != NULL) {
for (int i = 0; i < size; i++) {
printf("Iveskite skaiciu \n");
if (scanf("%d", &A[i]) != 1) {
printf("Missing values\n");
break;
}
}
}
return A;
}
int main(void) {
int n, i, dazn;
int *A;
dazn = 0;
printf("Iveskite naturalu skaiciu: \n");// enter how many elements in an array we will have
scanf("%d", &n);
A = allocate_and_read_array(n);
... // sort the array
... // find the most common element
free(A);
return 0;
}

How to print array returned from function

This code is supposed to recieve to arrays and then call function to return them in 1 array but I don't know how to print the last array returned from the function thanks in advance ???
and now I write anything because it says that the post is mostly code :D :D
#include <stdio.h>
#include <stdlib.h>
int join_arrays(int *array1, int *array2, int arr1_size, int arr2_size);
int main() {
int size_arr1, size_arr2, i, num1 = 1, s;
printf("Please enter the size of the first array: ");
scanf("%d", &size_arr1);
int arr1[size_arr1];
printf("start fill your first array: \n");
for (i = 0; i < size_arr1; i++) {
printf("enter element number %d: ",num1);
scanf("%d", &arr1[i]);
num1++;
}
num1 = 1;
printf("Please enter the size of the second array: ");
scanf("%d", &size_arr2);
int arr2[size_arr2];
int *ptr1_last;
printf("start fill your second array: \n");
for (i = 0; i < size_arr2; i++) {
printf("enter element number %d: ", num1);
scanf("%d", &arr2[i]);
num1++;
}
ptr1_last = join_arrays(arr1, arr2, size_arr1, size_arr2);
printf("sorted array= \n");
for (s = 0; s < (size_arr1 + size_arr2); s++) {
printf("%d\n", ptr1_last);
}
return 0;
}
int join_arrays(int *array1, int *array2, int arr1_size, int arr2_size) {
int counter_arr1, counter_arr2, m = 0;
int last_arr[arr1_size + arr2_size];
for (counter_arr1 = 0; counter_arr1 < arr1_size; counter_arr1++) {
last_arr[counter_arr1]=array1[counter_arr1];
}
for (counter_arr2 = counter_arr1; counter_arr2 < (arr1_size + arr2_size); counter_arr2++) {
last_arr[counter_arr2] = array2[m];
m++;
}
return last_arr[0];
}
Modified the code to create the receiving array in main and pass a pointer to it to the merge function because the local array last_arr would no longer exist when the function returned in your code.
#include <stdio.h>
#include <stdlib.h>
//Prototype changed to include a pointer to the receiving array, also no longer returns a value.
void join_arrays(int *last_arr, int *array1,int *array2,int arr1_size,int arr2_size);
int main()
{
int size_arr1,size_arr2,i,num1=1,s;
printf("Please enter the size of the first array: ");
scanf("%d",&size_arr1);
int arr1[size_arr1];
printf("start fill your first array: \n");
for(i=0; i<size_arr1; i++)
{
printf("enter element number %d: ",num1);
scanf("%d",&arr1[i]);
num1++;
}
num1=1;
printf("Please enter the size of the second array: ");
scanf("%d",&size_arr2);
int arr2[size_arr2];
int *ptr1_last;
printf("start fill your second array: \n");
for(i=0; i<size_arr2; i++)
{
printf("enter element number %d: ",num1);
scanf("%d",&arr2[i]);
num1++;
}
int last_arr[size_arr1 + size_arr2]; //Create receiving array here
join_arrays(last_arr, arr1,arr2,size_arr1,size_arr2); //And pass it to the function.
printf("merged array= \n");
for(s=0;s<(size_arr1+size_arr2);s++)
{
printf("%d\n", last_arr[s]);
}
return 0;
}
void join_arrays(int *last_arr, int *array1,int *array2,int arr1_size,int arr2_size)
{
int counter_arr1, m=0;
for(counter_arr1=0; counter_arr1<arr1_size; counter_arr1++)
{
last_arr[counter_arr1]=array1[counter_arr1];
}
for(; counter_arr1<(arr1_size+arr2_size); counter_arr1++)
{
last_arr[counter_arr1]=array2[m];
m++;
}
}
With that function you return only the first element of the last_array, you should create a global array so it's visible in all functions, or return a pointer of the last_array[0] position in memory

Resources