Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What is the C code for mergeing two unsorted arrays without using a 3rd array.
Eg
array1={1,3,5,7}
array2={2,4,6}
Output should be
array1={1,2,3,4,5,6,7}.
This is the code i have written.But this doesnt work if the number of array elements are same in both arrays.kindly help me in fixing this bug.
void merge(int a[],int b[],int ele1,int ele2)
{
int i,j,k,ele3;
ele3=ele1+ele2;
for(i=1,k=0;k<ele2;i=i+2)
{
j=ele1;
while(j>=i)
{
a[j]=a[j-1];
j--;
}
a[j+1]=b[k];
k++;ele1++;
}
for(i=0;i<ele3;i++)
printf("%d ",a[i]);
}
main()
{
int a[]={1,3,5},b[]={2,4},ele1,ele2;
ele1=sizeof(a)/sizeof(a[0]); ele2=sizeof(b)/sizeof(b[0]);
merge(a,b,ele1,ele2);
}
It can be done easily if any one of the array is capable of holding all elements, Otherwise it can be done if any one of the array is dynamic array, In case of dynamic array we can change the size of the array by using realloc so, after that we merge it easily
The solution will be like [Not tested, Its likely answer]
void merge(int ar1[], int ar2[])
{
int len = ar1.length;
int totalLength= (ar1.length+ar2.length);
ar1= (int*)realloc(ar1,(totalLength)*sizeof(int));
for (int i=len,index=0; i<totalLength; i++)
{
ar1[i]=ar2[index];
index++;
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}
Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Question: Given an amount m and an array of numbers b[] print "Yes" if m can be formed by addition of any number of elements from the array without repeating them.
This is what I've come up with; how can I add a memoization matrix into it?
#include<stdio.h>
int check(int b[],int n,int m){
if(m==0){
return 1;
}
if(m<0){
return 0;
}
if(n<=0 && m>0){
return 0;
}
return check(b,n-1,m-b[n-1]) + check(b,n-1,m);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d",&n);
scanf("%d",&m);
int b[n];
for(int i=0;i<n;i++){
scanf("%d",&b[i]);
}
if(check(b,n,m)>0){
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
}
Can any one help me put a memorization matrix into it ?
The key to the memoization cache consists of the first n elements of b, plus m. Form a string from these, and use it as a key to a hash table.
...But what's the point? With n decreasing in each recursion, there are only two situations where memoization will help:
When a non-recursive call check is made for exactly the same list of numbers and m as an earlier call to check.
When m-b[n-1] == m, which is to say when b[n-1] == 0.
The first situation is unlikely (and decreasingly so as the size of the list increases, which is when memoization would help the most), and the second is easy to avoid. Just replace
return check(b,n-1,m-b[n-1]) + check(b,n-1,m);
with
return b[n-1] == 0
? check(b,n-1,m) * 2
: check(b,n-1,m) + check(b,n-1,m-b[n-1]);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
(Write a C function that should check if a matrix (4x5) is sparse or not. Knowing that: sparse matrix is a matrix that has zeros more than the half of its size.)
That's a problem for a sheet in out subject
here is my code:
#include <stdio.h>
#include <stdlib.h>
int Spare(int [4][5]);
int main()
{
int arr[4][5];
int m,n;
for(m=0;m<4;m++){
for(n=0;n<5;n++){
scanf("%d ",&arr[4][5]);
}
}
Spare(arr[4][5]);
return 0;
}
int Spare(int Arr[4][5]){
int i,j;
int zerocount=0;
for(i=0;i<4;i++){
for(j=0;j<5;j++){
if(Arr[i][j]==0){
zerocount++;
}
}
}
if(zerocount>=10) return 1;
else return 0;
}
Its Running but after the user enter inputs of array it stops working!
Any Help Guys?
Change scanf("%d",&arr[4][5]) into scanf("%d",&arr[m][n]). You are just storing the input in arr[4][5] everytime in the loop.
The Spare(arr[4][5]) pass the element of fifth row and sixth column only which doesn't exist.
The Spare function expects an array as parameter and you are passing the single element only.
The function call must be done in this way: Spare(arr)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wanna get some numbers from keyboard. But how to store that number without array[] ? Have i chance to do that ? I don't know exact how many numbers come from keyboard. If i had permission of array, its simple. But array is not allowed.
In your situation, I'd still go with arrays, but if you insist on using pointers, this code below will help you. Regardless of whether you need arrays or pointers, you still need to define an upper limit on how many elements can be stored in memory.
It is now up to you to modify the code to make it efficient and pretty to your assignment needs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int numelements=10;
int curelement=0;
int* data=calloc(1,numelements*sizeof(int));
int* p=data;
int* res=data;
while (curelement < numelements){
scanf("%d",p);
if (*p==0){break;} //exit if number entered is zero.
p++;
curelement++;
}
//print results
while(*res != 0){
printf("%d ",*res);
res++;
}
free(data);
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use a function that displays a block of memory's content pointed by a pointer.
But didn't get the desired output, i am new to this, please correct my if i am wrong.
when I input size =3, element = 1,2,3 , I got output = 1 only.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
void merge(int **arr1);
int main(void) {
int size1;
printf("Give me the size of first array\n");
scanf("%d", &size1);
int *arr1 = malloc(size1*sizeof(int));
int *p1=arr1;
printf("Give me the elements of first array\n");
int index1;
for(index1 = 0 ; index1<size1; index1++)
scanf("%d", p1++);
merge(&arr1);
return;
}
void merge(int **arr1) {
while(**arr1) //**arr1 is the content of the passed array, if there
// is an int in it, print that out and increment to next one
{
printf("%d", **arr1); // ** is the content and * is the address i think, right?
*arr1++;
}
}
Your merge() code expects the array to be terminated by zero. The calling code is not doing it, so the behavior is unspecified (I got segfault when I tried your code).
The other issue is that you should put parentheses around *arr1:
(*arr1)++;
When I run your code with this modification and enter zero for the last element, your code runs fine.