I want to find the largest element in an array using call by reference method that is functions and pointers. I am getting an error at function call. Here is my try:
#include <stdio.h>
void bigg(int *a[10],int *N);
int main()
{
int a[10],i,N,p;
printf("Enter the total number of elements in the array:\n");
scanf("%d",&N);
printf("Enter the elements in the array one by one:\n");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
bigg(&a,&N);
}
void bigg(int *a[10],int *N)
{
int i,max;
max = *a[0];
for(i=0;i<*N;i++)
{
if( *a[i] > max)
{
max = *a[i];
}
}
printf("The biggest element in the given array is: %d",max);
}
Your basics of pointer is not clear.
For this question it works.
#include <stdio.h>
void bigg(int a[],int *N);
int main()
{
int a[10],i,N,p;
printf("Enter the total number of elements in the array:\n");
scanf("%d",&N);
printf("Enter the elements in the array one by one:\n");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
bigg(a,&N);
}
void bigg(int a[],int *N)
{
int i,max;
max = a[0];
for(i=0;i<*N;i++)
{
if( a[i] > max)
{
max = a[i];
}
}
printf("The biggest element in the given array is: %d",max);
}
What you're passing doesn't match the expected argument.
The function bigg is expecting for it's first argument an int *[10] which is an array of pointers. You're passing it &a which is a pointer to an array and has type int (*)[10].
You actually don't want either of these. An array name is, in most contexts, converted to a pointer to its first element. So if you pass that you'll have access to the elements. So change your function definition to accept an int * for the first argument (and an int for the second since you're not changing N):
void bigg(int *a, int N)
{
int i,max;
max = a[0];
for(i=0;i<N;i++)
{
if( a[i] > max)
{
max = a[i];
}
}
printf("The biggest element in the given array is: %d",max);
}
And call it like this:
bigg(a,N);
Related
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);
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");
}
}
In below code, in main function if I remove "p = createHeap(n,a)", then its taking input normally till defined n. But if its not removed then array input (for loop) will go on taking input continuously (infinite loop). Not able to get the reason.
#include<stdio.h>
int * createHeap(int n, int *b);
void insert();
void delete();
int main() {
int n,i,j;
int *p;
printf("Enter the size of array : ");
scanf("%d",&n);
int a[n];
printf("\nEnter the elements of array: %d",n);
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
}
printf("out");
p = createHeap(n,a); // Problem in this line.
//If removed then array will take input upto n
// if not then input loop will be infinite.
printf("\nsuccess");
return 0;
}
int * createHeap(int n, int *b) {
int i,j,k;
for(i=1;i<n;i++) {
j=(i+1)/2 - 1; //parent node
k=i;
while(j>=0) {
if(b[k] > b[j])
{
b[i] = b[i]+b[j]; //swap
b[j] = b[i]-b[j];
b[i] = b[i]-b[j];
}
k=j;
j=(i+1)/2 - 1; //tracking parent node
}
}
return b;
}
I am trying to output different elements from two arrays. So if i have an array A: {9, 0, 1} and B is {0, 8, 1}, I need to output an element which included in the first set, but are not included in the second :9 Can not think how I should compare all elements from the first array with the second one.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10],b[10],c,n,i,j;
printf("enter a number: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter a[%d]: ",i+1);
scanf("%d",&a[i]);
}
printf("\n");
for(j=0;j<n;j++){
printf("Enter b[%d]: ",j+1);
scanf("%d",&b[j]);
}
for (i = 0; i < n; i++) {
printf("%d ", a[i]); }
printf("\n");
for (i = 0; i < n; i++) {
printf("%d ", b[i]); }
printf("\n");
return 0;
}
I'd like to show my thoughts but i think it's stupid:
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(a[i]!= b[j]){
c=a[i];
}
}
printf("%d ",c);
}
This can be easily solved using Binary search. Follow the simple steps.
Step 1: Sort the second array.
Step 2: For each element of the first array, binary search it in the second array, if its not present , print it, otherwise dont.
The time complexity is O(m log n), where m is length of first array and n is length of second array.
If you want a more efficient solution, as suggested by #Sumeet Singh, you can sort the second array with qsort, then find similar elements from the first array with bsearch(binary search).
Your current solution is O(N^2) time, which will be very slow with large n, but you can achieve more efficiency with this approach.
Here is some code I wrote up with demonstrates this:
#include <stdio.h>
#include <stdlib.h>
#define NNUMBERS 10
void get_array_input(int array1[], int array2[], size_t *n);
void search_elements(int array1[], int array2[], size_t n);
void print_arrays(int array[], size_t n);
int cmp_func(const void *a, const void *b);
int main(void) {
int array1[NNUMBERS], array2[NNUMBERS];
size_t n;
/* input from user */
get_array_input(array1, array2, &n);
printf("\nFirst array: ");
print_arrays(array1, n);
printf("\nSecond array: ");
print_arrays(array2, n);
/* sorting the second array */
qsort(array2, n, sizeof(*array2), cmp_func);
printf("\nSorted Second array: ");
print_arrays(array2, n);
/* the search begins */
search_elements(array1, array2, n);
return 0;
}
void get_array_input(int array1[], int array2[], size_t *n) {
size_t i;
printf("Enter n: ");
if (scanf("%zu", n) != 1) {
printf("Invalid n value.\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < *n; i++) {
printf("Enter array1[%zu]: ", i);
if (scanf("%d", &array1[i]) != 1) {
printf("Invalud array value.\n");
exit(EXIT_FAILURE);
}
}
for (i = 0; i < *n; i++) {
printf("Enter array2[%zu]: ", i);
if (scanf("%d", &array2[i]) != 1) {
printf("Invalud array value.\n");
exit(EXIT_FAILURE);
}
}
}
void search_elements(int array1[], int array2[], size_t n) {
size_t i;
void *key;
printf("\nElements in first array which are not in second array: ");
for (i = 0; i < n; i++) {
key = bsearch(&array1[i], array2, n, sizeof(*array2), cmp_func);
if (!key) {
printf("%d ", array1[i]); /* not found, so print it */
}
}
printf("\n");
}
void print_arrays(int array[], size_t n) {
size_t i;
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
/* cmp function needed for qsort and bsearch */
/* many ways to write these */
int cmp_func(const void *a, const void *b) {
const int *num1 = (const int *)a;
const int *num2 = (const int *)b;
if (*num1 > *num2) {
return +1;
} else if (*num1 < *num2) {
return -1;
}
return 0;
}
Input:
Enter n: 3
Enter array1[0]: 9
Enter array1[1]: 0
Enter array1[2]: 1
Enter array2[0]: 0
Enter array2[1]: 8
Enter array2[2]: 1
Output:
First array: 9 0 1
Second array: 0 8 1
Sorted Second array: 0 1 8
Elements in first array which are not in second array: 9
You are on the right path. You are taking each value from the first array and comparing to each value in the second.
What you need to do now is to only print a[i] if there isn't any b[j] such that they are the same. The easiest way is to set a flag (say, unique=1). You can give this flag any name you find suitable, but in this case I'm thinking it says that the number a[i] is "unique" to the array a. So in this case you start with the premise that, yes, you won't find a[i] in the arrayb, and then you try to disprove your assumption. If at any time of you search you find an instance of a[i] == b[j], then your premise was wrong, so you set unique=0.
After you have compared this a[i] against all elements in b, you review your flag. And you print the appropriate message depending on whether you found this element in b or not.
Note that this assumes that the same value doesn't appear twice in a.
I have edited your code a little bit and this code gives you desired output:
#include <stdio.h>
int main(void){
int a[10],b[10],c,n,i,j;
int counter=0;
printf("enter a number: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter a[%d]: \n",i+1);
scanf("%d",&a[i]);
}
printf("\n");
for(j=0;j<n;j++){
printf("Enter b[%d]: \n",j+1);
scanf("%d",&b[j]);
}
for(i=0;i<n;i++){
counter=0;
for(j=0;j<n;j++){
if(a[i]!=b[j]){
counter++;
}
}
if(counter == n){
printf("%d ",a[i]);
}
}
return 0;
}
Let's explain this code a little bit:
In the last nested for loop, outer loop takes one element from array a. Inner loop gets every element of array b in order to compare it to taken element from array a. If none of the elements of array b is equal to a's taken element, counter will be equal to n(array size). Then we can print this element taken from a(it means there is no match between this taken element and array b's all of elements.
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");
}
}