c program finding call function - c

Two integers are stored in the arrays a 1 and a 2, respectively, and the product is calculated by the same procedure as the calculation, but it does not output the correct result.
question is : want to produce 312*321 = 1 0 0 1 5 2 but this first program produce
? 0 9 9 11 5 2
to produce right result 1 0 0 1 5 2, call function name func(c,N*2)
#include <stdio.h>
#include <stdlib.h>
#define N 3
int main()
{
int a1[N]={1,2,3};
int a2[N]={2,1,3};
int b[N][N];
int c[N*2];
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++)
b[i][j]=a1[j]*a2[i];
}
c[0]=b[0][0];
c[1]=b[0][1]+b[1][0];
c[2]=b[0][2]+b[1][1]+b[2][0];
c[3]=b[1][2]+b[2][1];
c[4]=b[2][2];
for(i=N*2-1;i>=0;i--)
{
printf("%d ",c[i]);
}
printf("\n");
return 0;
}
The result:0 9 9 11 5 2
  |0|1|2| ->A1
----------------
A2<-| 0|2|4|6|
| 1|1|2|3|
| 2|3|6|9|
this array is the same as 321*312 calculate using hand in paper
Problem: Define the function func () to output the correct result 1 0 0 1 5 2, call func (c, N * 2); below i post the code with the call function func() in bold. any idea?? and also what the logic behind func()? trial and error? is there algorithm behind this?
#include <stdio.h>
#include <stdlib.h>
#define N 3
int main()
{
int a1[N]={1,2,3};
int a2[N]={2,1,3};
int b[N][N];
int c[N*2];
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++)
b[i][j]=a1[j]*a2[i];
}
c[0]=b[0][0];
c[1]=b[0][1]+b[1][0];
c[2]=b[0][2]+b[1][1]+b[2][0];
c[3]=b[1][2]+b[2][1];
c[4]=b[2][2];
**func(c,N*2);**
for(i=N*2-1;i>=0;i--)
{
printf("%d ",c[i]);
}
printf("\n");
return 0;
}
**void func(int a[],int digit)
{
here no idea....
}**

Try with this;
void func(int a[], int digit)
{
int i, c = 0;
for(i = 0; i < digit; i ++)
{
a[i] += c;
c = a[i] / 10;
a[i] = a[i] % 10;
}
}

I think you should change two things:
First initialization the result array
int c[N * 2] = {0}; //initialize
Also the function looks like
void func(int a[], int size) {
int carry = 0;
for (int i = 0; i < size; i++) {
a[i] += carry;
carry = a[i] / 10;
a[i] = a[i] % 10;
}
}

Related

What is the bug in this in c qsort function

I am trying to run same calculate function with c++ and it works as expected but not with c, could you please tell what is wrong. I am just sorting two arrays comparing value and incrementing answer;
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
int compare_function(const void *a,const void *b) {
int *x = (int *) a;
int *y = (int *) b;
return *x - *y;
}
int calculate(long long A[], long long G[],int t){
int answer = 0;
int j = 0;
int i = 0;
while (i < t) {
if(G[i] > A[j]){
i++;
j++;
answer++;
} else {
i++;
}
}
return answer;
}
int main()
{
int p;
scanf("%d", &p);
while (p > 0){
int t;
scanf("%d", &t);
long long G[t];
long long A[t];
for(int i=0;i<t;i++)
scanf("%d",&G[i]);
for(int i=0;i<t;i++)
scanf("%d",&A[i]);
qsort(G,t,sizeof (long long ),compare_function);
qsort(A,t,sizeof (long long ),compare_function);
printf("%d\n", calculate(A,G,t));
p--;
}
}
I have used c(gcc 8.2.0) and one weird thing is every time I ran this program I got differ output.
Array Input
1
10
3 6 7 5 3 5 6 2 9 1
2 7 0 9 3 6 0 6 2 6
output: 6, expected : 7
constraints
1<= N <=100000
0<= Array elemts <= LLONG_MAX

Find partial sum of 'X' numbers in array in c

can you help me with code which returns partial sum of 'X' numbers in array in c?
Complete :
int arr_sum( int arr[], int n )//Recursive sum of array
{
if (n < 0) //base case:
{
return arr[0];
}
else
{
return arr[n] + arr_sum(arr, n-1);
}
}
void sum_till_last (int *ar,int si )
{
**int sum,i;// the problem some where here
ar=(int*) malloc(si*sizeof(int));
for (i=0; i<si;i++)
{
sum=arr_sum(ar,i);
ar [i]=sum;
}
free (ar);**
}
void main ()
{
int i;
int a [5];
for (i = 0; i < 5; i++)
scanf_s("%d", &a[i]);
sum_till_last(a,5);
printf("%d\n",a[5]);
}
\i want to create new array with this this legality:
My input :
4
13
23
21
11
The output should be (without brackets or commas):
4
17
40
61
72
Now when we can see the full code, it's quite obvious that the problem is in the sum_till_last function where you overwrite the pointer you pass to the function with some new and uninitialized memory you allocate.
Drop the allocation (and the call to free of course). And fix the logical bug in arr_sum that causes you to get arr[0] + arr[0] when i is zero.
Here you go:
#include <stdio.h>
int main () {
int in_arr[5] = {4,13,23,21,11};
int out_arr[5];
int p_sum =0;
int i;
for ( i=0;i<5;i++){
out_arr[i] = in_arr[i]+p_sum;
p_sum=p_sum+in_arr[i];
}
for (i=0;i<5;i++){
printf("%d", out_arr[i] );
}
}
Fix according to your policy
#include <stdio.h>
#include <stdlib.h>
int arr_sum(int arr[], int n){
if (n == 0){//Change to this
return arr[0];
} else {
return arr[n] + arr_sum(arr, n-1);
}
}
void sum_till_last(int *ar, int si){
int sum,i;
int *temp = malloc(si * sizeof(int));//variable name ar is shadowing parameter name ar.
for(i = 0; i < si; i++){
temp[i] = arr_sum(ar, i);
if(i)
putchar(' ');
printf("%d", temp[i]);//need print out :D
}
free(temp);
}
int main(void){
int i, a[5];
for (i = 0; i < 5; i++)
scanf_s("%d", &a[i]);
sum_till_last(a, 5);
//printf("%d\n",a[5]);<-- this print only one element. and It's out of bounds element XD
}
I just made it simple so it´s easy to understand :)
I´m assuming "n" is always equal or less then array element number. Then you just print the SUM.
#include <stdio.h>
int arr_sum( int arr[], int n ){
int i=0,SUM=0;
for(; i < n;i++){
SUM= SUM+ arr[i];
printf("%d ",SUM);
}
}
int main(void) {
int array[] = {4, 13, 23, 21, 11};
arr_sum(array,5);
return 0;
}

Shell sort in C not giving required result

I am required to implement a Shell sort in C and use the optimised version (where the gap is first set to size of array/2 and then this number is repeatedly divided by 2.2). The problem is that the answer isn't always completely sorted and I am not sure whether this is because of some logic error in my code or some shortcoming of the Shell sort.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define MAX 7
void shellSort(int *a, int size);
void insert(int *a, int next_pos, int gap);
void shellSort(int *a,int size)
{
int gap = floor(size/2);
int next_pos;
while (gap>0)
{
for(next_pos = gap; next_pos < size; next_pos++)
insert(a, next_pos, gap);
if(gap == 2)
gap = 1;
else
gap = (int)(gap/2.2);
}
}
void insert(int *a, int next_pos, int gap)
{
int value = a[next_pos];
while(next_pos >= gap && a[next_pos] < a[next_pos-gap])
{
a[next_pos] = a[next_pos-gap];
next_pos = next_pos - gap;
}
a[next_pos] = value;
}
int main()
{
int nums[MAX];
time_t t;
srand((unsigned) time(&t)); // seed randomiser
printf("Array before sorting:\n");
int i;
for(i=0; i<MAX; i++)
{
nums[i] = rand() % 101; // filling array with random numbers
printf("%d\t", nums[i]);
}
shellSort(nums, MAX);
printf("\nArray after sorting:\n");
for(i=0; i<MAX; i++)
printf("%d\t", nums[i]);
return 0;
}
The following is my output:
Array before sorting:
74 26 1 12 38 81 94
Array after sorting:
12 1 26 38 74 81 94
edit: posted before I was done with my question oops
I think your problem lies here:
int value = a[next_pos];
while(next_pos >= gap && a[next_pos] < a[next_pos-gap])
Since you're changing next_pos in the lines below, i think those lines should read (if my understanding of shellsort is correct):
int value = a[next_pos];
while(next_pos >= gap && value < a[next_pos-gap])

Combinations of elements in an array

I'm trying to write a C program to find all the combinations of an given array and a specified length. This is what I've done so far..
#include <stdio.h>
void com(int* a, int* t, int len, int i) {
int j, k;
if(len == 0) {
for(k=0;k<3;k++) {
printf("%d ",t[k]);
}
printf("\n");
return;
}
for(j = i ; j <= 4-len ; j++) { // 4 = original array size
t[3-len] = a[j];
com(a,t,len-1,i+1);
}
}
main() {
int t[3];
com((int[]){4,1,3,2},&t[0],3,0); // 3 = combination length
}
The problem in this code is that it has no option to skip duplicates, repetitions of combination. e.g for the array {1,2,3,4} it generates
1 2 3
1 2 4
1 3 3
1 3 4
2 2 3
2 2 4
2 3 3
2 3 4
but it was supposed to generate
1 2 3
1 2 4
1 3 4
2 3 4
What can I do for that? I'm not sure how to do that. Any kind of help would be appreciated. Thanks.
Also, if there is an alternative and better optimized solution than this, feel free to share.
sample to fix
void com(int *a, int *t, int len, int i){
if(i == len){
for(int k = 0; k < len; k++)
printf("%d ", t[k]);
printf("\n");
return;
}
while(*a){
t[i] = *a;
com(++a, t, len, i+1);
}
}
int main(void){
int t[3];
com((int[]){1,2,3,4, 0}, t, 3, 0);
// ^end mark
return 0;
}
#include <stdio.h>
int check(int *t)
{
int j,k;
for(k=0;k<3;k++)
{
for(j=k+1;j<3;j++)
{
if(t[k]==t[j])
return 0;
}
}
return 1;
}
void com(int* a, int* t, int len, int i) {
int j, k;
int comb=1;
if(len == 0)
{
comb = check(t);
if(comb)
{
for(k=0;k<3;k++)
{
printf("%d ",t[k]);
}
printf("\n");
}
return;
}
for(j = i ; j <= 4-len ; j++) { // 4 = original array size
t[3-len] = a[j];
com(a,t,len-1,i+1);
}
}
main() {
int t[3];
com((int[]){4,1,3,2},&t[0],3,0); // 3 = combination length
}
Sorry for bad editing..

Change position of an exact number of elements in an array

Let's say we have an array of m elements and we want to change randomly the position of exactly n of them, where of course 2 <= n <= m.
For example: if we have this array of 10 ints {1 2 3 4 5 6 7 8 9 10} and we ask for 4 of its elements to change positions randomly, a result could be {3 2 1 4 5 6 10 8 9 7}
What is the simplest way to program this in ANSI C? (pseudocode will also be just fine)
Step1). Generate a list of n random unique numbers between 1 and m. This list should NOT be sorted. Eg, for your example, the list could have been [10,7,1,3]
Step 2) do something like :
int save = array[list[0]];
For (i=0; i<n-1; i++) {
Array[list[i]] = array[list[i+1]];
}
Array[list[n-1]] = save;
Edit: actually, you'll have to have subtract 1 from each list[whatever] to allow for zero-based arrays - but I'm sure you get the idea :)
since at least two numbers must be swapped, i would pick two random numbers from array first and then swap them. they are added to array that holds swapped indexes. if there's more to swap, pick another number different than swapped ones and swap it with one of the previously swapped numbers.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void print_arr(int a[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d ",a[i]);
}
printf("\n");
}
void swap(int a[], int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
int next_idx(int swapped[], int s_count, int size) {
int n, i;
char in_arr;
while (1) {
in_arr = 0;
n = rand() % size;
for (i = 0; i < s_count; i++) {
if (swapped[i] == n) {
in_arr = 1;
break;
}
}
if (!in_arr) {
break;
}
}
return n;
}
void swap2_or_more(int a[], int size,int count) {
srand(time(NULL));
int i, j, s_count = 0;
int swapped[size];
i = rand() % size;
swapped[s_count] = i;
s_count++;
do {
j = rand() % size;
} while (i == j); // make sure indexes are different
swapped[s_count] = j;
s_count++;
swap(a, i, j);
count -= 2;
while (count) {
i = next_idx(swapped, s_count, size);
j = rand() % s_count;
swap(a, i, swapped[j]);
swapped[s_count] = i;
s_count++;
count--;
}
printf("swapped indexes:\n");
print_arr(swapped, s_count);
}
int main(void)
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
swap2_or_more(a, 10, 5);
printf("array after swap:\n");
print_arr(a, 10);
return 0;
}

Resources