This program is supposed to take 2 arrays and perform the dot product on each of the elements in the array.
My program is fine if the index of n is less than 5; however, once the index of the array is greater than 5 only the first element in the first array is wrong ( I checked by adding a printf statement in the function). I don't know how to fix this bug.
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
int v1[n];
int v2[n];
int v3[n];
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
printf("Enter numbers for the first array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
Correct code
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
int v1[n],v2[n],v3[n]; //you didn't initialize n
printf("Enter numbers for the first array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d ", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
Related
I do not understand what is wrong with this code of Linear search. It compiles but on execution exits without output.
turns - no. of test cases.
size - size of array.
x - element to be searched.
#include <stdio.h>
int linearSearch(int arr[], int size, int element)
{
int i = 0;
for(i=0; i< size; i++)
{
if(arr[i] == element)
{
return i;
}
}
return 0;
}
int main()
{
int turns, size;
scanf("%d", &turns);
while(turns--)
{
scanf("%d", &size);
int arr[size];
for(int j=0; j < size; j++)
{
scanf("%d", arr[j]);
}
int x;
scanf("%d", &x);
int k = linearSearch(arr, size, x);
}
return 0;
}
There is one major problem in your code.
First you need to pass address of your array element(&arr[j]).
And the output is not displaying because you are not printing it out.
The correct code is
#include <stdio.h>
int linearSearch(int arr[], int size, int element)
{
int i = 0;
for(i=0; i< size; i++)
{
if(arr[i] == element)
{
return i;
}
}
return 0;
}
int main()
{
int turns, size;
scanf("%d", &turns);
while(turns--)
{
scanf("%d", &size);
int arr[size];
for(int j=0; j < size; j++)
{
scanf("%d", &arr[j]);
}
int x;
scanf("%d", &x);
int k = linearSearch(arr, size, x);
printf("%d\n", k);
}
return 0;
}
I'm very new to C and I can't quite figure out this problem.
I have to use the following function to copy every third element in input array a1[] of length n into output array a2[]
void decimate_by3(int a1[], int n, int a2[])
In the main function:
Ask user to input length of array, and elements in the array.
Calculate length of output array and declare output array.
Call decimate_by3 function
Display output array
I also have constraints which are:
I have to use the defined `void decimate_by3(int a1[], int n, int a2[]) function as is (no modifications)
I am not allowed to declare any other functions
I'm not sure where i'm going wrong but i've attached my work so far.. any help is appreciated :)
#include <stdio.h>
void decimate_by3 (int a1[], int n, int a2[])
int main (void)
{
int n, i;
printf("Enter the length of the array: ");
scanf("%d", &n);
int a1[n];
printf("Enter %d numbers: ", n);
{for (i = 0; i < n; i++)
scanf("%d", &a1[i]);}
int a2[];
if (n % 3 == 0)
int a2[n/3];
decimate_by3 (a1, n, a2);
printf("Output: ");
for (i = 0; i <= (n/3); i++)
{printf(" %d", a2[i]);}
else
int a2[(n/3)+1];
decimate_by3 (a1, n, a2);
printf("Output: ");
for (i = 0; i < n; i++)
{printf(" %d", a2[i]);}
printf("\n");
return 0;
}
void decimate_by3 (int a1[], int n, int a2[])
{
int i;
for (i = 0; i < n; i++)
{
if (i % 3 == 0)
a1[i] = a2[i];
else
delete;
}
}
You will get an error in this line because of a missing semi-colon:
void decimate_by3 (int a1[], int n, int a2[])
You also cannot declare an empty array in C. So this will raise an error:
int a2[];
This code should achieve what you want to do:
#include <stdio.h>
void decimate_by3 (int a1[], int n, int a2[]);
int main (void){
int n, i;
printf("Enter the length of the array: ");
scanf("%d", &n);
int a1[n];
printf("Enter %d numbers: \n", n);
for (i = 0; i < n; i++){
printf("Enter a number: ");
scanf("%d", &a1[i]);
}
int a2[n/3];
decimate_by3(a1, n, a2);
for(i = 0; i < n/3; i++){
printf("%d ", a2[i]);
}
printf("\n");
return 0;
}
void decimate_by3(int a1[], int n, int a2[]){
int i, j = 0;
for(i = 0; i < n; i++){
if((i+1)%3 == 0){
a2[j++] = a1[i];
}
}
}
You're almost there, couple tweaks here and there voila..
void decimate_by3(int a1[], int n, int a2[]);
int main(void) {
int n;
printf("Enter the length of the array: ");
scanf("%d", &n);
if(n <= 0)
return 0;
int a1[n];
printf("Enter %d numbers: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &a1[i]);
int a2[n / 3 + 1];
decimate_by3(a1, n, a2);
printf("Output: ");
for (int j = 0; j < n/3+1; j++)
printf("%i ", a2[j]);
return 0;
}
void decimate_by3(int a1[], int n, int a2[]) {
int cnt = 0;
for (int i = 0; i < n; i++)
if (i % 3 == 0)
a2[cnt++] = a1[i];
}
i have tried doing it with functions and arrays but have failed in-order to do it with pointers.
I want to do it with the help of pointers instead of arrays, but i am having problem with passing and calling the values of arrays using pointers.
below is the code.
#include <stdio.h>
void final_array(int arr[], int size);
void array(int arr[], int i, int size);
int main()
{
int num, size[100];
int i, j;
int arr[100][100];
printf("Enter the number of arrays: \t");
scanf("%d", &num);
num = num < 100 ? num: 100;
//feeding elements.
for (i = 0; i<num; i++)
{
printf("\nEnter the size of the array: \t");
scanf("%d", &size[i]);
printf("\nEnter the array: ");
size[i] = size[i] < 100 ? size[i] : 100;
array(&arr[i][0], i, size[i]);
}
for(i=0; i<num; i++)
{
final_array(&arr[i][0], size[i]);
printf("\n");
}
printf("\nPress Enter key to exit.\n");
getchar();
return 0;
}
void array(int arr[], int i, int size)
{
int j;
for (j = 0; j<size; j++)
{
printf("\nEnter arr[%d][%d]: \t",i, j);
scanf("%d", &arr[j]);
}
}
void final_array(int arr[], int size)
{
int j;
for(j=0; j<size; j++)
{
printf("%d\t", arr[j]);
}
}
I guess this should solve your problem.Let me know if it's correct.
#include <stdio.h>
#include <stdlib.h>
void final_array(int *arr, int size);
void array(int *arr, int i, int size);
int main()
{
int num;
int i, j;
int **arr=(int **)malloc(100 * sizeof(int *));
for (i=0; i<100; i++)
arr[i] = (int *)malloc(100 * sizeof(int));
printf("Enter the number of arrays: \t");
scanf("%d", &num);
int *size=malloc(sizeof(int)*num);
num = num < 100 ? num: 100;
//feeding elements.
for (i = 0; i<num; i++)
{
printf("\nEnter the size of the array: \t");
scanf("%d", &size[i]);
printf("\nEnter the array: ");
size[i] = size[i] < 100 ? size[i] : 100;
array(&arr[i][0], i, size[i]);
}
for(i=0; i<num; i++)
{
final_array(&arr[i][0], size[i]);
printf("\n");
}
printf("\nPress Enter key to exit.\n");
getchar();
return 0;
}
void array(int *arr, int i, int size)
{
int j;
for (j = 0; j<size; j++)
{
printf("\nEnter arr[%d][%d]: \t",i, j);
scanf("%d", &arr[j]);
}
}
void final_array(int *arr, int size)
{
int j;
for(j=0; j<size; j++)
{
printf("%d\t", arr[j]);
}
}
I am trying to do a selection sort using function called min().
This is my code:
#include <stdio.h>
#include <conio.h>
void main() {
int i, temp, arr[20], n, loc;
int min(int [], int, int);
printf("Enter a range of the array");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter elements");
scanf("%d", arr[i]);
}
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
min(int arr[], int i, int n) {
int j, loc, temp;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = j;
}
}
return (temp);
}
getch();
}
the compiler is giving one error when compiling.
it saying:
Error SELECTIONSORT.C 22: Expression Syntax.
my line number 22 is min(int arr[],int i, int n) according to my compiler Turbo C++.
Please guide me where I am going wrong.
Thanks for any help.
There are multiple problems in your code:
The function min must be defined outside the body of the main() function.
Note that it is considered bad style to declare function prototypes in a local scope. Either define the function before the main() function or put the prototype before the main() function.
Also the prototype for main() without arguments should be int main(void).
In function min, you must initialize temp to i, or use i directly.
You should print the array contents after the sort, otherwise the program has no effect.
Here is a corrected version:
#include <stdio.h>
#include <conio.h>
int min(int [], int, int);
int main(void) {
int i, temp, arr[20], n, loc;
printf("Enter a range of the array: ");
if (scanf("%d", &n) == 1) {
for (i = 0; i < n && i < 20; i++) {
printf("Enter element %d: ", i);
if (scanf("%d", &arr[i]) != 1)
break;
}
n = i; // n is the actual number of inputs
for (i = 0; i < n; i++) {
loc = min(arr, i, n);
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
for (i = 0; i < n; i++) {
printf("%d\n" array[i]);
}
}
getch();
return 0;
}
int min(int arr[], int i, int n) {
int j;
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
i = j;
}
}
return i;
}
Need help with this code it should return c[] with the number's of a[] % b[] = 0 but it doesn't work.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int *divide(int a[], int a_size, int b[], int b_size)
{
int i = 0, j = 0, k = 0, counter = 0, *c;
c = (int*)malloc(b_size * sizeof(int));
for (i = 0; i < b_size; i++)
{
for (j = 0; j < a_size; j++)
{
if (a[j] % b[i] == 0)
counter++;
}
c[k] = counter;
k++;
counter = 0;
}
for (int t = 0; t < b_size; t++)
{
printf("%d ", c[t]);
}
printf("\n");
}
main ()
{
int *a, *b, a_size, b_size;
printf("Enter size of a:\n");
scanf ("%d", &a_size);
a = (int*)malloc(a_size * sizeof(int));
printf("\nEnter size of b:\n");
scanf("%d", &b_size);
b = (int*)malloc(b_size * sizeof(int));
printf("\nEnter elements of a:\n");
for (int i = 0; i < a_size; i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter elements of b:\n");
for (int j = 0; j < b_size; j++)
{
scanf("%d", &b[j]);
}
divide(&a, a_size, &b, b_size);
}
There was some errors in your code.
You want your fonction *divide(...) to return a new array containing the operation a[i] % b[i], but your function doesn't return anything, so you have to return it. It seems more logical to print the new array in the main, than in the function while doing it.
When you pass the variables to your function be careful to pass int*, not int **.
Here is a sample of code which works (you didn't say what to do if a_size and b_size were different so I assume we only use the smallest size and don't treat the number after) :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int *divide(int a[], int a_size, int b[], int b_size)
{
int i = 0, stop, *c;
if (b_size <= a_size)
{
c = (int*)malloc(b_size * sizeof(int));
stop = b_size;
}
else
{
c = (int*)malloc(a_size * sizeof(int));
stop = a_size;
}
while (i < stop)
{
c[i] = a[i] % b[i];
i++;
}
return (c);
}
int main ()
{
int *a, *b, a_size, b_size;
int *result = NULL;
int stop;
int i = 0;
printf("Enter size of a:\n");
scanf ("%d", &a_size);
a = (int*)malloc(a_size * sizeof(int));
printf("\nEnter size of b:\n");
scanf("%d", &b_size);
b = (int*)malloc(b_size * sizeof(int));
printf("\nEnter elements of a:\n");
for (int i = 0; i < a_size; i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter elements of b:\n");
for (int j = 0; j < b_size; j++)
{
scanf("%d", &b[j]);
}
result = divide(a, a_size, b, b_size); //not &a neither &b because it would be a char** instead of a char*
if (a_size < b_size)
stop = a_size;
else
stop = b_size;
while (i < stop)
{
printf("%d ", result[i]);
i++;
}
return 0;
}