Two array merge into a third array in c - arrays

For some Reason the Program Crashes at the Loop used to merge the two arrays in main.
though maybe this question is a foolish in one(idk it didn't allow me to post as my program is mostly code so now i am blabbering).
(Well it still doesn't so here's a Picture of a Cat
CAT )
//To make two arrays of user Defined Size and merge them in a third array
#include<stdio.h>
#include<stdlib.h>
void array(int**,int);
void ini(int**,int);
void display(int **,int);
int main()
{
int *a1,*a2,*a3,s1,s2,i,j;
printf("Enter Size of Array 1\n");
scanf("%d",&s1);
printf("Enter Size of Array 2\n");
scanf("%d",&s2);
ini(&a1,s1);
ini(&a2,s2);
ini(&a3,s1+s2);
printf("Enter Elements of Array 1\n");
array(&a1,s1);
printf("Array 1:\n");
display(&a1,s1);
printf("Enter Elements of Array 2\n");
array(&a2,s2);
printf("Array 2:\n");
display(&a2,s2);
for(i=0;i<s1;i++)
{
a3[i]=a1[i];
}
for(i=0,j=s1;i<s2&&j<s1+s2;i++,j++)
{
a3[j]=a2[i];
}
printf("Merged Array:\n");
display(&a3,s1+s2);
return 0;
}
void ini(int **a,int s)
{
*a=(int*)calloc(s,sizeof(int));
}
void array(int **a,int s)
{
int i;
for(i=0;i<s;i++)
{
printf("Enter Element at position %d\n",i+1);
scanf("%d",&a[i]);
}
}
void display(int **a,int s)
{
int i;
for(i=0;i<s;i++)
{
if(i==s-1)
printf("%d\n",a[i]);
else
printf("%d\t",a[i]);
}
}

a1, a2, and a3 is a pointer of an integer type variable, which stores the starting address of your buffer.
a1[index] is the value in the address of a1 with 'index' as the offset.
Since you were passing &a to your array() and display() function, you were passing the address of your variable into the function instead of passing the address of your buffer.
You could change your array() and display() to:
void array(int *a,int s)
{
int i;
for(i=0;i<s;i++)
{
printf("Enter Element at position %d\n",i+1);
scanf("%d", &(a[i]));
}
}
void display(int *a,int s)
{
int i;
for(i=0;i<s;i++)
{
if(i==s-1)
printf("%d\n", a[i]);
else
printf("%d\t", a[i]);
}
}
and call it like this:
array(a1,s1);
display(a1,s1);

Related

How to write two functions for getting an array and display that array in c

function for get array from the user
#include <stdio.h>
void getArray()
{
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
}
function for display array
void displayArray(){
printf("\nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
}
Both functions are called in the main
int main(){
int a[1000],i,n;
getArray();
displayArray();
return 0;
}
The problem is how to pass the array that we get from the user to the display array function and both functions can be called in the main and also the array want to declare in the main function
An example that does not handle input errors.
In order for your functions to have knowledge of the array, you must send them its address as well as its size.
#include <stdio.h>
int getArray(int a[], int size_max)
{
int n;
printf("Enter size of array: ");
while(1)
{
scanf("%d",&n);
if(n>size_max) printf("The size must be less than %d: ", size_max);
else break;
}
printf("Enter %d elements in the array : ", n);
for(int i=0; i<n; i++) scanf("%d", &a[i]);
return n;
}
void displayArray(int a[], int n)
{
printf("\nElements in array are: ");
for(int i=0; i<n; i++) printf("%d ", a[i]);
}
int main()
{
int a[1000];
int n = getArray(a, 1000);
displayArray(a, n);
return 0;
}
You can pass that shared variable as argument. Also return and use the returned value if reference not having valid data. Or else you need to pass this argument as reference instead of value like this.
#include <stdio.h>
int[] getArray(int a[])
{
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
return a;
}
function for display array
void displayArray(int a[]){
printf("\nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
}
Both functions are called in the main
int main(){
int a[1000],i,n;
a = getArray(a);
displayArray();
return 0;
}

Accepting values of array in a function and using it in another function, How is this Works in C?

in a program to accept an array and display it on the console using functions getArray() and displayArray() how this program works as it Accepts values of array in function getArray() and uses it in the function displayArray() without returning any values from the first function?
I tried this program and failed to get result, then found this one in youTube comment section and I tried it and got results! I want to know how this program works ?
Q:Write a program to accept an array and display it on the console using function?
a.Program should contain 3 functions including main() function,
main() - {
Declare an array.
Call function getArray().
Call function displayArray() }.
getArray() -
Get values to the array.
displayArray() -
Display the array values
#include <stdio.h>
#include <stdlib.h>
void getArray(int);
void displayArray(int);
int main(void) {
int limit;
printf("Enter The Size of the Array :");
scanf("%d",&limit);
getArray(limit);
displayArray(limit);
return EXIT_SUCCESS;
}
void getArray(int limit){
int i,a[100];
printf("Enter The Values of Array :\n");
for(i=0;i<limit;i++){
scanf("%d",&a[i]);
}
}
void displayArray(int limit){
int i,b[100];
printf("Your Array is :\n");
for(i=0;i<limit;i++){
printf(" %d\t",b[i]);
}
printf("\n");
}
Array a in getArray is a local variable that gets destroyed when it goes out of scope. Array b in displayArray is also a local variable (local to displayArray) and has no relationship with a in getArray. You need to pass the same array to both functions.
One way could be to allocate the needed memory for the array in main and pass that, along with the number of elements in the array, to the two functions.
Example:
#include <stdio.h>
#include <stdlib.h>
// a is now a pointer to the first element in the array:
void getArray(int *a, int limit) {
printf("Enter The Values of Array :\n");
for(int i = 0; i < limit; i++) {
scanf("%d", &a[i]);
}
}
// b is now a pointer to the first element in the array:
void displayArray(int *b, int limit) {
printf("Your Array is :\n");
for(int i = 0; i < limit; i++) {
printf(" %d\t", b[i]);
}
putchar('\n');
}
int main(void) {
int limit;
printf("Enter The Size of the Array :");
if(scanf("%d", &limit) != 1 || limit < 1) return EXIT_FAILURE;
// allocate memory for `limit` number of `int`s:
int *arr = malloc(limit * sizeof *arr);
if(arr == NULL) return EXIT_FAILURE;
getArray(arr, limit); // pass arr + limit
displayArray(arr, limit); // pass arr + limit
free(arr); // and free the memory when done
return EXIT_SUCCESS;
}
#include<stdio.h>
void getArray(int);
void displayArray(int);
int array[100];
void main()
{
int limit;
printf("enter the array size you want\n");
scanf("%d",&limit);
getArray(limit);
displayArray(limit);
}
void getArray(int limit)
{
printf("enter the array element you want\n");
for(int i=0;i<limit;i++)
{
scanf("%d",&array[i]);
}
}
void displayArray(int limit)
{
for(int i=0;i<n;i++)
{
printf("%d",array[i]);
printf("\t");
}
}

Storing **array into another array (function) in C

My task is to enter a sentence and every 2nd word is to be stored into a second array. I managed to send the array into a function, but now I don't know how to store words with strcpy.
First array is mat1 and second array is mat2.
void function(char **mat1, char *mat2, int n, int *j)
{
int i, j2=0;
printf("\n");
for(i=0;i<n;i++)
{
printf("%s ", mat1[i]);
printf("\n");
if(i==0 || i%2==0)
{
mat2[j2]=(char*)malloc(10);
strcpy(&mat2, mat1);
j2++;
}
}
}
In main, I did this:
char *mat1[10], mat2[10], mat3[10];
mat1[i]=(char*)malloc(10);
scanf("%s", mat1[i]);
function( mat1,&mat2[0], n, &j);
I'm not sure if I need to malloc mat2 and then to do something with strcpy. I need to return mat2 and printf words in main.
Firstly you should change type types of mat2 to which are similar to ones of mat1.
Then, allocate buffers and copy strings using array access to their elements.
void function(char **mat1, char **mat2, int n, int *j)
{
int i, j2=0;
printf("\n");
for(i=0;i<n;i++)
{
printf("%s ", mat1[i]);
printf("\n");
if(i==0 || i%2==0)
{
mat2[j2]=(char*)malloc(10);
strcpy(mat2[j2], mat1[i]);
j2++;
}
}
*j = j2; /* probably you want this */
}
char *mat1[10], *mat2[10], mat3[10];
function( mat1, mat2, n, &j);

using pointer subscript inside an array

i wrote a program for counting sort using pointers
#include<stdio.h>
#include<stdlib.h>
int max(int *arr,int n)
{
int i;
int maxi=*arr;
for(i=0;i<n;i++)
{
if(*(arr+i)>maxi)
maxi=*(arr+i);
}
return maxi;
}
void count(int *arr,int n)
{
int i;
int s=max(arr,n);
printf("max %d",s);
int c[s];
memset(c,0,s*sizeof(int));
int b[n];
for(i=0;i<n;++i)
{
++c[arr[i]];
}
for(i=1;i<s;++i)
{
c[i]=c[i]+c[i-1];
}
for(i=n-1;i>=0;--i)
{
b[c[arr[i]]]=arr[i];
--c[arr[i]];
}
for(i=0;i<n;++i)
{
arr[i]=b[i];
}
}
void print(int *arr,int n)
{
int i;
for(i=0;i<n;i++)
printf("\nelement is %d ",*(arr+i));
}
int main()
{
int n,i;
int *arr=malloc(n*sizeof(int));
printf("\nenter the number of elements ");
scanf("%d ",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
count(arr,n);
print(arr,n);
return 0;
}
as i debug the program it stops at ++c[arr[i]] giving a segmentation fault. My call watches window looks like this
my inputs were like this
enter the number of elements 4
1
3
5
7
max 7
the code received segmentation fault after this also, the value of i is initialized to 0,then why is the watches window showing it's value as 4. is it fine to pass pointer subscripts to a static array or should i declare c dynamically?

2D array passing to a function

I've been reading this question but I'm not able to get the resulting code to solve the problem.
How should I change this in order to make it work?
void print2(int ** array,int n, int m);
main()
{
int array[][4]={{1,2,3,4},{5,6,7,8}};
int array2[][2]={{1,2},{3,4},{5,6},{7,8}};
print2(array,2,4);
}
void print2(int ** array,int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i][j]);
printf("\n");
}
}
You are passing in a pointer to an array, but your function is expecting a pointer to a pointer. In C, the array name decays to a value that is the pointer to the first array element. In this case, the first array element is an array, so the function argument decays to a pointer to an array.
Here is one way you can fix this. Change the function to take a void * so that the dimension does not interfere with the argument. Then the dimension argument is used in the function body to create the proper pointer type for the 2D array.
void print2(void *p,int n,int m)
{
int i,j;
int (*array)[m] = p;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i][j]);
printf("\n");
}
}
If you are willing to change the order of the arguments, then you can use the proper type for the array argument:
void print2(int n, int m, int array[n][m])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i][j]);
printf("\n");
}
}
Since Jack asked about C89, here's a way to handle it. Since a 2D array is organized the same as a long 1D array in memory, you can just walk the passed in pointer as such. Again, we accept the input parameter as a void * to avoid dealing with the decayed type. Then, we treat the pointer as a long 1D array, but we walk it according to the proper dimensions:
void print2(void *p, int n, int m)
{
int i,j;
int *array = p;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i*m+j]);
printf("\n");
}
}
In your question you are passing arguments as pointer to an array.
Do as given below :
void print2(int (*array)[4],int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",array[i][j]);
printf("\n");
}
}

Resources