How to append elements to an array in a program in C? - c

I want the user to input values of elements to an array of 10 ints. Then I want to print out all the indexes of the values of the elements which when divided by 5 give 0.
I've tried doing it like this, but it's not working. How do I append the i's into an array?
#include <stdio.h>
#define N 10
int main()
{
int a[10];
int i;
int index[10] = {};
printf("Input %d values into the array:\n", N);
for (i = 0; i < N; i++){
scanf("%d", &a[i]);
if (a[i] % 5 == 0){
index[10] += i;
}
}
printf("%d", index);
return 0;
}

You can create a new index for the second array and solve the problem. But is better to make it in different loops.
#include <stdio.h>
#define N 10
int main()
{
int a[N];
int index[N] = {};
//o will be the index for index array
int o = 0;
printf("Input %d values into the array:\n", N);
for (int i = 0; i < N; i++){
scanf("%d", &a[i]);
if (a[i] % 5 == 0){
index[o] = i;
o++;
}
}
for(int i = 0; i<o;i++){
printf("%i",index[i]);
}
return 0;
}

Your last comment clarified the intention of your code so here is how solve it directly without the restoring to dynamic memory allocation:
#include <stdio.h>
#define N 10
int main(void) {
int result[N];
int n = 0;
for (int i = 0; i < N; i++){
int d;
if(scanf("%d", &d) != 1) {
printf("scanf failed\n");
return 1;
}
if (!(d % 5)) {
result[n++] = i;
}
}
for(int i = 0; i < n; i++) {
printf("%d%s", result[i], i + 1 < n ? "," : "\n");
}
}
and example run:
echo "1 2 3 4 5 6 7 8 9 10" | ./a.out
4,9

Related

Problem with program to list primes below integer using arrays and nested loops

#include <stdio.h>
#include <math.h>
#define MAX_SIZE 10000
int main(void)
{
int a[MAX_SIZE];
int N;
int L; /* the current size of the list */
/* read in the upper limit. Keep reading until
a valid number between 3 and the maximum that
can be handled by the array is entered */
double b[10000];
int j, i;
L = 0;
printf("Enter the upper limit:\n");
do {
scanf("%d", &N);
} while (N<3 || N>MAX_SIZE+2);
int prime;
for (j = 1; j < N; j++)
{
prime = 1;
for (i = 2; i < j; i++)
{
if (j % i == 0)
{
prime = 0;
break;
}
}
if (prime)
{
a[i] = j;
L++;
}
}
/* write out the result - DO NOT CHANGE THIS */
for(i=0;i<L;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
Program needs to take an integer, calculate primes below that integer, print that list of primes.
I think my problem is related to the loops.
The program is calculating the primes but listing 0 if the number previously there isnt prime eg a[4] is now printing as 0
Any help is appreciated.
thanks.
Is this what you were trying to implement?
#include <stdio.h>
#define MAX_SIZE 100
int main(void)
{
int primes[MAX_SIZE];
int primes_found = 0;
int limit = 0;
while (limit < 3)
{
printf("Enter the upper limit:\n");
scanf("%d", &limit);
}
for (int candidate = 2; candidate <= limit && primes_found < MAX_SIZE; candidate++)
{
int divisor = 2;
int is_prime = 1;
while(is_prime && divisor < candidate)
is_prime = candidate % divisor++ != 0;
if (is_prime)
primes[primes_found++] = candidate;
}
for (int i = 0 ; i < primes_found ; i++)
printf("%d ", primes[i]);
printf("\n");
return 0;
}

count array value bug

Hi I need help please i need to fix this code so it count the value only once
for exmaple
input:
25
38 25 36 4 1 1 10 37 45 21 37 42 21 1 50 9 50 42 6 39 10 14 17 11 20
10
36 42 2 15 28 42 3 23 8 50
output:
4
the answer here should be 4 not 7.
#include <stdio.h>
int main()
{
int n, m, count = 0;
int array[1000];
int subarray[1000];
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
scanf("%d", &subarray[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (array[i] == subarray[j])
count++;
}
}
printf("%d\n", count);
}
Possible solution, using functions and qsort.
(untested code)
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int cmpfunc (const void * a, const void * b) {
return (*(int*)a > *(int*)b) - (*(int*)a < *(int*)b);
}
int main() {
int n, m, count = 0;
int array[1000];
int subarray[1000];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
qsort(array, n, sizeof(int), cmpfunc); // O(n lg n)
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d", &subarray[i]);
int result = binarySearch(arr, 0, n - 1, x); // O(lg n)
if (result != -1)
count++;
} // O(m lg n)
printf("%d\n", count);
}
You need to keep track of the matched values in third array as following and
check if the new value is found before or not
#include <stdio.h>
int main()
{
int n, m, count = 0;
int array[1000];
int subarray[1000];
int result[1000];
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
scanf("%d", &subarray[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (array[i] == subarray[j]){
int isFound=0;
for(int k=0;k<count;k++){
if(result[k]==array[i])
isFound=1;
}
if(isFound==0){
result[count]==array[i];
count++;
}
}
}
}
printf("%d\n", count);
}
I use a boolean variable to check if the elements of array 1 exist in the array 2 ,if not ,i will copied it to array 3
#include <stdio.h>
#include <stdbool.h>//header how found the booleen variable
int main()
{
int n,m;
do
{
printf("Give me the length of array 1 :");
scanf("%d",&n);
}while(n<1);
int T1[n];//declaration of Array 1
do
{
printf("Give me the length of array 2 :");
scanf("%d",&m);
}while(m<1);
int T2[m];//declaration of Array 2
printf("The fill of Array 1:\n\n");
for(int i=0;i<n;i++)
{
scanf("%d",&T1[i]);
}
printf("The fill of Array 2:\n\n");
for(int j=0;j<m;j++)
{
scanf("%d",&T2[j]);
}
int T3[n+m];//declaration of Array 3
bool found=false;//declaration of booleen variable
int k=0;
for(int i=0;i<n;i++)
{found=false;
for(int j=0;j<m;j++)
{
if(T1[i]==T2[j])
{
found=true;
}
}
if(found==true)
{
T3[k]=T1[i];
k++;
}
}
printf("\nThe number of elment duplicate is :%d\n",k);
}
so it count the value only once
With minimal impact to OP's code and without re-ordering input:
Check if the subarray[] value already occurred within itself.
When subarray[]/array[] values match, quit looking.
for (int j = 0; j < m; j++) {
bool unique_subarray_value = true;
for (int earlier = 0; earlier < j; earlier++) {
if (subarray[earlier] == subarray[j])
unique_subarray_value = false;
break;
}
}
if (unique_subarray_value) {
for (int i = 0; i < n; i++) {
if (array[i] == subarray[j]) {
count++;
break;
}
}
}
}
A fast approach would sort the arrays and then walk them
// Pseudo code
sort array[n] with qsort()
sort subarray[m] with qsort()
// walk the arrays looking for matches
i = 0;
j = 0;
while (i < n && j < m) {
if (array[i] == subarray[j]) {
count++;
while (i + 1 < n && array[i] == array[i+1]) i++;
while (j + 1 < m && subarray[j] == subarray[j+1]) j++;
}
if (array[i] < subarray[j]) i++;
else j++;
}

Swapping largest and smallest numbers in C

I want to write a program that reads 10 int values from the user and swaps the largest and smallest numbers on the first and second values, then the rest of the numbers should be in the order.
Please check the code and help me what the wrong is.
For instance:
1
9
4
5
6
7
8
2
4
5
New order should be 9 1 4 5 6 7 8 2 4 5
#include <stdio.h>
int main() {
int a[10],i,min,max=0,pos=0;
printf("Please enter 10 int values :\n");
do{
scanf("%d", &a[pos++]);
} while (pos<10);
for (i=0; i<10;i++) {
printf("%i\n",a[i]);
if (max<a[i])
{
max=a[i];
}
if (min>a[i])
{
min=a[i];
}
for (i=0;i<10;i++) {
if (a[i]==max)
a[i]=max;
if (a[i] == min) a[i] = min;
}
printf("The new order is : %d %d %d ", max, min, ...);
return 0;
}
EDIT:
It is the new form
#include <stdio.h>
int main() {
int a[10],i,pos,temp,min = 0,max = 0;
printf("Please enter 10 int values :\n");
do {
scanf("%d", &a[pos++]);
} while (pos < 10);
for ( =1; i<10;i++) {
if (a[i]>a[max])
{
max=i;
}
if (a[i]<a[min])
{
min=i;
}
}
temp=a[max];
a[max]=a[min];
a[min]=temp;
printf("%d %d",a[max],a[min]);
for (i=0;i<10;i++){
if ((i != min) && (i != max)) {
printf("%d ", a[i]);
}
}
printf("\n");
return 0;
}
As others have noted, your code does not properly identify the maximum and minimum values in the array because you are writing min and max back into the array instead of the other way around.
Since you want to swap these values, what you actually want are the indices of the min and max values of the array, and swap those.
It is best to break this code into functions instead of having everything in main. Here is a solution that will do what you want:
#include <stdio.h>
int indexofmax(int *data, int len)
{
int max = 0;
int i;
for(i = 0; i < len; i++)
{
if(data[i]>data[max]) max = i;
}
return max;
}
int indexofmin(int *data, int len)
{
int min = 0;
int i;
for(i = 0; i < len; i++)
{
if(data[i]<data[min]) min = i;
}
return min;
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
// user enters in 10 ints...
int max = indexofmax(a, 10);
int min = indexofmin(a, 10);
int i;
swap(&a[min], &a[max]);
for(i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
This initialization min=0,max=0 is not right.
Instead have min = INT_MAX and max = INT_MIN.
By setting min=0, you would never get the lowest number in the array if it is greater than 0.
Similarly by setting max=0, you would never get the greatest number in the array if it is lower than 0.
You are gaining nothing by this code:
for(i=0;i<10;i++)
{ if(a[i]==max) a[i]=max;
if(a[i]==min) a[i]=min; }
It is evident that this loop
for(i=0;i<10;i++)
{ if(a[i]==max) a[i]=max;
if(a[i]==min) a[i]=min; }
does not make sense.
Moreover variable min is not initialized while variable max is initialized incorrectly.
int a[10],i,min,max=0,pos=0;
For example the array can contain all negative elements. In this case you will get incorrect value of the maximum equal to 0.
And I do not see where the elements are moved to the right to place the maximum and the minimum to the first two positions of the array.
If I have understood correctly then what you need is something like the following. To move the elements you could use standard function memmove declared in header <string.h>. However it seems you are learning loops.
#include <stdio.h>
#define N 10
int main( void )
{
int a[N] = { 4, 5, 9, 6, 7, 1, 8, 2, 4, 5 };
for (size_t i = 0; i < N; i++) printf("%d ", a[i]);
printf("\n");
size_t min = 0;
size_t max = 0;
for (size_t i = 1; i < N; i++)
{
if (a[max] < a[i])
{
max = i;
}
else if (a[i] < a[min])
{
min = i;
}
}
if (max != min)
{
int min_value = a[min];
int max_value = a[max];
size_t j = N;
for (size_t i = N; i != 0; --i)
{
if (i - 1 != min && i - 1 != max)
{
if (i != j)
{
a[j - 1] = a[i - 1];
}
--j;
}
}
a[--j] = min_value;
a[--j] = max_value;
}
for (size_t i = 0; i < N; i++) printf("%d ", a[i]);
printf("\n");
}
The program output is
4 5 9 6 7 1 8 2 4 5
9 1 4 5 6 7 8 2 4 5
You're not actually altering the array.
In the second loop, you say "if the current element is the max, set it to the max". In other words, set it to its current value. Similarly for the min.
What you want is to swap those assignments.
if(a[i]==max) a[i]=min;
if(a[i]==min) a[i]=max;
Also, your initial values for min and max are no good. min is unitialized, so its initial value is undefined. You should initialize min to a very large value, and similarly max should be initialized to a very small (i.e. large negative) value.
A better way to do this would be to keep track of the index of the largest and smallest values. These you can initialize to 0. Then you can check a[i] > a[max] and a[i] < a[min]. Then you print the values at indexes min and max, then loop through the list and print the others.
int i, temp, min=0, max=0;
for (i=1; i<10; i++) {
if (a[i] > a[max]) max = i;
if (a[i] < a[min]) min = i;
}
printf("%d %d ", a[max], a[min]);
for (i=0; i<10; i++) {
if ((i != min) && (i != max)) {
printf("%d ", a[i]);
}
}
printf("\n");
Just keep it nice and simple, like this:
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 10
int find_biggest(int A[], size_t n);
int find_smallest(int A[], size_t n);
void print_array(int A[], size_t n);
void int_swap(int *a, int *b);
int
main(void) {
int array[MAXNUM], i, smallest, biggest;
printf("Please enter 10 int values:\n");
for (i = 0; i < MAXNUM; i++) {
if (scanf("%d", &array[i]) != 1) {
printf("invalid input\n");
exit(EXIT_FAILURE);
}
}
printf("Before: ");
print_array(array, MAXNUM);
smallest = find_smallest(array, MAXNUM);
biggest = find_biggest(array, MAXNUM);
int_swap(&array[smallest], &array[biggest]);
printf("After: ");
print_array(array, MAXNUM);
return 0;
}
int
find_biggest(int A[], size_t n) {
int biggest, i, idx_loc;
biggest = A[0];
idx_loc = 0;
for (i = 1; i < n; i++) {
if (A[i] > biggest) {
biggest = A[i];
idx_loc = i;
}
}
return idx_loc;
}
int
find_smallest(int A[], size_t n) {
int smallest, i, idx_loc;
smallest = A[0];
idx_loc = 0;
for (i = 1; i < n; i++) {
if (A[i] < smallest) {
smallest = A[i];
idx_loc = i;
}
}
return idx_loc;
}
void
print_array(int A[], size_t n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
void
int_swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}

C program for below pyramid If possible JAVA code also [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Desired Output
1234554321
1234__4321
123____321
12______21
1________1
Code
#include<stdio.h>
int main()
{
int num,c,sp,r=1;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
printf("\n");
for(; num>=1; num--,r++)
{
for(c=1; c<=num; c++)
printf("%d",c);
for(sp=r; sp>1; sp--)
printf("_");
for(sp=r; sp>1; sp--)
printf("_");
for(c=num; c>=1; c--)
printf("%d",c);
printf("\n");
}
}
I am looking for other alternative codes which are less complex. Any help would be appreciated.
Rather than making multiple calls to printf, it seems cleaner to construct the string yourself:
#include<stdio.h>
#include<stdlib.h>
int
main( int argc, char ** argv )
{
unsigned num = argc > 1 ? strtol( argv[1], NULL, 0 ) : 5;
char digits[] = "123456789_987654321";
char *rhs = digits + sizeof digits - 1 - num;
if( num > 9 ) {
fprintf( stderr, "argument must be < 10" );
exit( EXIT_FAILURE );
}
digits[num] = '\0';
for( ; num > 0; ) {
printf( "%s%s\n", digits, rhs );
digits[ sizeof digits - 1 - num ] = '_';
digits[ --num ] = '_';
}
return 0;
}
Your lines are symmetrical. You can use a recursive function where you print before and after recursing to achieve this:
#include <stdlib.h>
#include <stdio.h>
void line(int i, int n, int m)
{
if (i < n) {
putchar(i > m ? '_' : '0' + (i + 1) % 10);
line(i + 1, n, m);
putchar(i > m ? '_' : '0' + (i + 1) % 10);
}
}
int main(int argc, char **argv)
{
int i, n = 0;
if (argc > 1) n = atoi(argv[1]);
if (n <= 0) n = 5;
i = n;
while (i--) {
line(0, n, i);
putchar('\n');
}
return 0;
}
The lines themselves are controlled with a regular loop. The newline '\n' can't be part of the recursive function, because it doesn't fit the symmetric pattern; it has to be printed explicitly. This version takes the size of the pyramid from the command line with a default of 5.
Something like this should do:
#include <stdio.h>
int main()
{
int num;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
printf("\n");
for (int row = 0; row < num; ++row) {
int columnLimit = num - row;
for (int column = 1; column <= columnLimit; ++column) {
printf("%d", column);
}
for(int spacing = 0; spacing < row; ++spacing) {
printf("__");
}
for (int column = columnLimit; column > 0; --column) {
printf("%d", column);
}
printf("\n");
}
}
IDE One
#include<stdio.h>
int main()
{
int num,c,sp;
printf("\n");
for(num =5; num>=1; num--)
{
for(c=1; c<=5; c++)
{
if(c>num)
{
printf("_");
}
else
{
printf("%d",c);
}
}
for(c=5; c>=1; c--)
{
if(c>num)
{
printf("_");
}
else
{
printf("%d",c);
}
}
printf("\n");
}
}
Use ternary operator for simplification, this way you only need two lines
inside of the inner loop code,
val = column <= num ? column : limit - column + 1;
printf("%c", val > row ? '_' : val + 0x30);
Here it is:
int main(){
int num,column,row,limit,val;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
printf("\n");
limit = num * 2;
for (row = num; row > 0; --row){ //num times
for (column = 1; column <= limit; ++column){
val = column <= num ? column : limit - column + 1;
printf("%c", val > row ? '_' : val + 0x30);
}
printf("\n");
}
return 0;
}
The downside is you still have to call printf multiple times. But obviously the code is really compact!
A revisited one that asks the user for the max number...
int main()
{
int num;
printf("Enter loop repeat number(rows, 9 max): ");
scanf("%d",&num);
if (num > 9) num = 9;
int i,j;
for (i=num ; i>0 ; i--) {
for(j=1 ; j<=num ; j++) {
printf("%c", j>i ? '_':0x30+j);
}
for(j=num ; j>=1 ; j--) {
printf("%c", j>i ? '_':0x30+j);
}
printf("\n");
}
return 0;
}
0x30 is the ascii code for 0, adding j gives 1 to num.
edit a recursive bonus ...
int num; // global to avoid putting it in the recursive function
void recursive(int i, int j) {
printf("%c", i>j ? '_':0x30+i);
if (i<num) recursive(i+1, j);
printf("%c", i>j ? '_':0x30+i);
if(i==1) {
printf("\n");
if (--j > 0) recursive(1, j);
}
}
To be called as recursive(1, num);
int main(int argc, char **argv)
{
printf("Enter loop repeat number(rows, 9 max): ");
scanf("%d",&num);
if (num > 9) num = 9;
recursive(1, num);
return 0;
}
Assuming (but not checking!) that input does not exceed 9:
#include<stdio.h>
int main()
{
int num, r, c;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
printf("\n");
for(r=num; r>=1; row--)
{
for(c=1; c<=num; c++)
putc(c<=r ? ('0'+c) : '_');
for(c=num; c>=1; c--)
putc(c<=r ? ('0'+c) : '_');
putc("\n");
}
}
The question seems to assume that it won't go above an input of 9, so I'll assume the same ...
int num=5; // Set as required
int max=9;
char leftNums[10] = "123456789";
char rightNums[10] = "987654321";
char pyramid[17] = "________________";
int rightStart = max - num;
for (int row=0; row<num; row++) {
int colLimit = num - row;
printf("%*.*s", colLimit, colLimit, leftNums);
printf("%*.*s", 2*row, 2*row, pyramid);
printf("%*.*s\n", colLimit, colLimit, &rightNums[rightStart+row]);
// Or, if you prefer for the last line:
// printf("%*.*s\n", colLimit, colLimit, rightNums+rightStart+row);
}
Notes:
You can obviously easily play around with letters, numbers and so on in this approach.
If you want to go above 9, you'll probably want to use letters rather than numbers ...
IDEone.com
This works until 9 rows:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int num, cols;
int i, j;
printf("Enter loop repeat number(rows): ");
scanf("%d",&num);
printf("\n");
if (num<10)
{
cols=num*2;
char **matrix = malloc(num*sizeof(char *));
for(i=0; i<num; i++)
{
matrix[i] = malloc(cols*sizeof(char *));
memset(matrix[i], '_', cols);
for (j=0; j<num; j++)
{
if (j<(num-i))
{
matrix[i][j] = (j+1)+0x30;
matrix[i][cols-j-1] = matrix[i][j];
}
}
}
for (i=0; i<num; i++)
{
for (j=0; j<cols; j++)
printf("%c", matrix[i][j]);
printf("\n");
free(matrix[i]);
}
free(matrix);
}
else
{
printf("Max allowed rows = 9\n");
}
return 0;
}
IDE One

print an array that contains arr1's numbers which are not in arr2

I wrote a program that:
gets integers to arr1[3] by user's input
get another input - called max number.
arr2[3] gets random 3 numbers ranging from 0 to maxNumber.
for e.g. if I enter number 4, then arr2[] will contain 3 integers from 0 to 3 (included).
4.Build arr3[3] which contains arr1[3] integers that ARE NOT in arr2[3] for e.g. if arr1[3] = 1 2 3, arr2[3] = 2 5 6, then arr3[3] = 1 3 garbage here
Print arr3[].
Weird problem: It prints only arr3[0] which is arr1[2]'s value, for some reason. Any suggestions?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define N 3
void inputArr1(int arr1[]);
void maxValueC(int maxValue);
void inputArr2(int arr2[], int maxValue);
void inputArr3(int arr1[], int arr3[], int i, int count3);
void printArr3(int arr1[], int arr3[]);
void main()
{
int i = 0, j = 0, count = 0, count3 = 0, maxValue = 0, arr1[N], arr2[N], arr3[N];
inputArr1(arr1);
printf("please enter maxValue: ");
scanf("%d", &maxValue);
while (maxValue <= 0)
{
maxValueC(maxValue);
}
srand(time(NULL));
inputArr2(arr2, maxValue);
printf("\nArray1: ");
for (i = 0; i < N; i++)
{
printf("%d ", arr1[i]);
}
printf("\nArray2: ");
for (i = 0; i < N; i++)
{
printf("%d ", arr2[i]);
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (arr1[i] != arr2[j])
{
count++;
}
}
if (count == N)
{
inputArr3(arr1, arr3, i, count3);
}
count = 0;
}
printArr3(arr1, arr3);
getch();
}
void inputArr1(int arr1[])
{
int i;
for (i = 0;i < N;i++)
{
printf("Please enter a digit[%d]: ",i);
scanf("%d", &arr1[i]);
printf("\n");
}
}
void maxValueC(int maxValue)
{
printf("Please enter a number that is bigger than 0!!!");
scanf("%d", &maxValue);
}
void inputArr2(int arr2[], int maxValue)
{
int i;
for (i = 0; i < N; i++)
{
arr2[i] = rand() % (maxValue);
}
}
void inputArr3(int arr1[], int arr3[], int i, int count3)
{
arr3[count3] = arr1[i];
count3++;
}
void printArr3(int arr3[])
{
int i;
printf("\nArray3: ");
for (i = 0; i < N; i++)
{
printf("%d", arr3[i]);
}
}
When you call inputArr3() you pass count3 as argument by value (it goes to your stack) and later you manipulate the value (in the stack) but your count3 variable (defined in main) will never be altered.
You probably want to pass pointer to count3, for example:
void inputArr3(int arr1[], int arr3[], int i, int *count3)
{
...
(*count3)++;
}
When you call the function you have to change it to as follows:
inputArr3(arr1, arr3, i, &count3);
Another problem is when you call printArr3 - you call it with 2 arguments while it takes one. I suggest you change the call to printArr3 as follows:
printArr3(arr3);
Do not forget to modify your function prototypes.
I hope this is what you need, because is hard to understand:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define N 3
int main(void){
int arr1[N];
int arr2[N];
int arr3[N];
int found = 0;
int a,b,i,j,k=0,t=0;
int maxNumber = 8;
srand((unsigned)time(NULL));
/* create Arr1 */
for (a = 0; a < N; a++){
arr1[a] = (rand() % maxNumber);
}
/* create Arr2 */
for (a = 0; a < N; a++){
arr2[a] = (rand() % maxNumber);
}
/* Print Arr1 */
printf("Arr1\n");
for (b = 0; b < N; b++){
printf("%d ",arr1[b]);
}
/* Print Arr2 */
printf("\nArr2\n");
for (b = 0; b < N; b++){
printf("%d ",arr2[b]);
}
/* create Arr3 */
for(i=0;i<N;i++){
for(j=0;j<N;j++){
if(arr1[t] == arr2[j]){
found++;
}
}
if(!found){
arr3[k]=arr1[t];
k++;
}
found=0;
t++;
}
/* Print Arr3 */
printf("\nArr3\n");
for(int u=0;u<k;u++){
printf("%d ",arr3[u]);
}
printf("\n");
return 0;
}
Output1:
Arr1
0 4 2
Arr2
6 1 2
Arr3
0 4
Output2:
Arr1
6 0 1
Arr2
0 7 5
Arr3
6 1
Output3:
Arr1
7 2 7
Arr2
7 5 6
Arr3
2

Resources