I have an array having 100 numbers say a[1,2,3,4,5,6....98,99,100]. I want to divide it into 25 groups with each group containing 4 elements and then subtract elements of other blocks with each element of one block. For example: if three blocks out of 25 are labelled as A,B,C and contain elements as:
A [1,2,3,4],
B[5,6,7,8] &
C[9,10,11,12]
then subtraction is to be done like this:
(A-B, A-C),
(B-A, B-C) &
(C-A, C-B)
i.e.
1-5,1-6,1-7,1-8,1-9,1-10,1-11,1-12; then
2-5,2-6,2-6,2-8,2-9,2-10,2-11,2-12; then
3-5,3-6,3-7,3-8,3-9,3-10,3-11,3-12; then
4-5,4-6,4-7,4-8,4-9,4-10,4-11,4-12;
THEN
5-1,5-2,5-3,5-4,5-9,5-10,5-11,5-12;
and like wise..
Can anyone help me in writing C program for this.
The code that I have written is partial and not doing the above task completely. Code is:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100]={1,2,3,4.....,98,99,100};
int i=0, j=0;
int x[100], y[100];
// considering only 12 numbers for the sake of simplicity
for (i=0;i<12;i++)
{
for(j=0;j<8;j++)
{
x[j] = a[i] - a[r+4];
}
y[i] = x[i];
}
}
#include <stdio.h>
int main(void) {
int N, i;
scanf("%d", &N);
int numArray[N]; // Define an array of four integers
// Get inputs for the array elements
for (i=0;i<N; i++) {
scanf("%d", &numArray[i]);
}
int sum = 0;
// Write here the logic to add these integers:
for (i=0;i<N;i++) sum += numArray[i];
printf("%d\n",sum); // Print the sum
return 0;
}
Related
I want to write a C program to adjust the carry in an integer array (i.e. convert the 2 digit number into a single-digit and add the carry to the next number).
For example -
Array - 6 12 3 15 7
Answer: 7 2 4 5 7
Here's my code:
#include<stdio.h>
int main(){
int array[6]={6,22,3,15,7};
int array2[2];
int i;
printf("%d\n",array[1]);
for(i=0;i<6;i++){
if(array[i]>10){
array2[i]=array[i];
printf("Value at %d element of array is: %d \n",i,array2[i]);
}
}
return 0;
}
So far I have been able to write a program that just finds out double-digit numbers in the array.
I'm relatively new to C and don't know much about how to perform arithmetic operations in arrays.
Help me please!!
since we are adding the carry to previous element, we should start in reverse order.
#include <stdio.h>
int main()
{
int i;
int a[5] = {6,22,3,15,7};
for(i=4;i>0;i--)
{
if(a[i]>9)
{
int rem = a[i]%10;
int carry = a[i]/10;
a[i] = rem;
a[i-1] = a[i-1] + carry;
}
}
if(a[0]>9)
a[0] = a[0]%10;
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
return 0;
}
Given an array of integers, find the sum of its elements.
my problem is that; in c language you have only fixed size array;
my code does not print the sum of random size array which the question demands;[1,2,3,4,5,-------n elements] it prints for only for ex 6 or defnite size aray; using a loop;
#include <stdio.h>
int main() {
int i;
scanf("%d\n",&i);
int a[6];
int sum=0;
for(i=0;i<=5;i++)
scanf("%d\n",&a[i]);
for(i=0;i<=5;i++)
sum=sum+a[i];
printf("%d\n",sum);
return 0;
}
It seems you want to let you user input the number of elements in the array. Your code scan that information into variable i. Therefore you can't use variable i as counter in the for loops. You need two different variables. One variable to hold the the number of integers to include in the sum and another variable for the loops.
Further to get a variable sized array, you need to use the users input when you define the array. This is called VLA (Variable Length Array).
Something like:
#include <stdio.h>
int main() {
int i;
int N = 0; // New variable holding the number of integers in the sum
scanf("%d\n",&N); // scan into N
int a[N]; // Use N for the VLA
int sum=0;
for(i=0;i<N;i++) // Use N as limit
scanf("%d\n",&a[i]);
for(i=0;i<N;i++) // Use N as limit
sum=sum+a[i];
printf("%d\n",sum);
return 0;
}
That said - be careful about VLAs. If the user inputs a high number for N a stack overflow may occur. If you want to use VLAs your code should enforce a maximum
limit for the users input.
It's typically better to use dynamic allocation instead of VLA. Like:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int N = 0;
scanf("%d\n",&N);
int *a = malloc(N * sizeof *a); // Dynamic allocation
if (a == NULL) exit(1); // Check for allocation failure
int sum=0;
for(i=0;i<N;i++)
scanf("%d\n",&a[i]);
for(i=0;i<N;i++)
sum=sum+a[i];
printf("%d\n",sum);
free(a); // Free allocated memory
return 0;
}
Some extra comments:
1) To calculate the sum you actually don't need an array. Just scan into some int and add it to sum. No need for storing it in an array first.
2) Always check the return value of scanf. Example: if (scanf("%d\n",&N) != 1) exit(1);
I have to pass two arrays ( A and B with dimensions of 5X4) to two functions called FUNCTION_1 and FUNCTION_2. Both of arrays columns and rows should be passed as POINTERS. FUNCTION_1 will take each element of A array and calculate the sum of prime factors of each element located in A( (with the help of another function called sumPrime), then it will store these sums in array B. FUNCTION_2 has to print both of A and B arrays. ( The normal numbers array, and the prime factors sums array). There are some additions in the program which are not important now but I am going to show them too in respect of clearness.
#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COL 4
# include <math.h>
int sumPrime(int number){
int factor = 2;
int sum=0;
while(1!=number){
if(number%factor==0){
number /= factor;
sum+=factor;
factor = 2;
continue;
}
factor++;
}
return sum;
}
int FUNCTION_1(int *a[][20],int *b[][20],int row, int col){
int c,d;
for(c=0;c<row;c++){
for(d=0;d<col;d++){
b[c][d]=sumPrime(a[c][d]);
return b[c][d];
}
}
}
void FUNCTION_2(int *x[][20],int *y[][20],int rows, int cols){
printf(" \n A matrix is :\n");
int e,f;
for(e=0;e<rows;e++){
for(f=0;f<cols;f++){
printf("A[%d][%d] is %d\n",e,f,x[e][f]);
}
}
printf("\n B matrix is:\n");
for(e=0;e<rows;e++){
for(f=0;f<cols;f++){
printf("A[%d][%d] is %d\n",e,f,FUNCTION_1(x,rows,cols,y,rows,cols));
}
}
}
int main(){
int A[ROW][COL]={0};
int B[ROW][COL]={0};
int x=1;
int i;
int j;
for(i=0;i<ROW;i++){
for(j=0;j<COL;j++){
A[i][j]=x;
x=x+2;
}
}
printf("%d",A[0][0]);
return 0;
}
When trying to perform FUNCTION_1 or FUNCTION_2 I get many errors or even when defining the functions. There is no problem with defining A matrix or prime function! HELP!!
Your definition for a matrix is wrong ..
Use :
func(int* a[20] , int cols)
in this case you will have 20 rows ,
or
func(int a[][20] , int rows)
And define some macro for the columns if it is fixed value .
Two-dimensional integer array (5x5) with only 0 and 1 values.
There are:
8 neighbours for any element in the middle of the matrix.
5 neighbours for the elements on the border (row=0, column=0, row=4 or column=4) of the matrix
3 neighbours for the elements on the corner of the matrix.
Block contains connected 1.
In this homework, you will write a complete C program to create a 2-D (5x5) array and fill the array with random numbers (0 or 1). Program will find the number of blocks in the matrix (5x5).
Here is my question, I fill array with numbers but I can't find how to connect 1s as block.
This is my code:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
void find(int arr[][5],int size);
int main()
{
int arr[5][5];
int i,j;
srand (time(NULL));
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
arr[i][j]=rand()%2;
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
find(arr,5);
}
void find(int arr[][5],int size)
{
int i,j,count=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
}
}
}
a 'brute' force algorithm would use the facts as you laid out.
I.E. 4 checks for the corners 12 checks for the edges, 9 checks for the interior blocks
and example corner check:
int countOfBlocks = 0;
if( arr[0][0] && arr[0][1] && arr[1][0] && arr[1][1] ) countOfBlocks++;
...
The code would then take 25 lines of 'if's, similar to the above
I've got a function thats able to produces 2 arrays, one when the index 'i' is even and one when the index 'i' is odd, so I end up with two arrays. The even 'i' array is called W_e and made of N elements, the odd 'i' array is called W_o and also made of N elements.
I now need to be merge these two arrays into another array Wn (which has 2*N elements) such that it looks like Wn=[W_e[0],W_o[0],W_e[1],W_o[1],...,W_e[N-1],W_o[N-1]] but I'm not sure how to do it. I tried to use nested loops but it didn't work. The arrays W_e and W_o are produced correctly according to my calculations, I'm just unable to combine the entries into one array.
This is what I have so far. I have not done anything in the main function except call the function which is giving me trouble "double *MakeWpowers(int N);". Please keep in mind this works for N>2, I have not yet dealt with N=1 or N=2.
Any help will be greatly appreciated. Thank you!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#define pi 4*atan(1)
double *MakeWpowers(int N);
void print_vector(double *x,int N);
double *make_vector(double *x,int N);
int main(void)
{ int N;
double *Wn;
printf("\n Please enter the size of the NxN matrix:\n");
scanf("%d",&N);
Wn=MakeWpowers(N);
//print_vector(Wn,N);
free(Wn);
return(0);
}
double *MakeWpowers(int N)
{
double *Wn,*W_e,*W_o;
int i,j;
Wn=make_vector(Wn, 2*N);
W_e=make_vector(W_e, N);
W_o=make_vector(W_o, N);
for(i=0;i<=N-1;i++)
{
if(i%2==0)
{
W_e[i]=cos((2*i*pi)/N);
}
else
{
W_o[i]=sin((2*i*pi)/N);
}
}
printf("\nThis is the even vector W_e:\n");
print_vector(W_e, N);
printf("\nThis is the odd vector W_o:\n");
print_vector(W_o, N);
for(j=0;j<2*N;j++)
{
if(j%2==0)
{Wn[j]=W_e[i];}
//++i;}
else
{Wn[j]=W_o[i];}
//++i;}
printf("\nthis is Wn:\n\n");
print_vector(Wn, 2*N);
//Wn[j]=W_o[i];
//j++;
}
return(Wn);
}
void print_vector(double *x,int N)
{
int i;
for (i=0; i<N; i++)
{
printf ("%9.4f \n", x[i]);
}
printf("\n");
}
double *make_vector(double *x,int N)
{ int i;
double xi;
x=(double *)malloc((N)*sizeof(double));
for(i=0;i<N;i++)
{
x[i]=(double)xi;
}
return(x);
}
Here's the general logic to it:
LET a, b, merge BE ARRAYS
FOR k IN 0..length(a)
merge[2*k] = a[i]
merge[2*k+1] = b[i]
RETURN merge
a makes the even entries (2k), b the odd ones (2k+1).
This is probably wrong
double *make_vector(double *x,int N)
{ int i;
double xi;
You have to initialize variable xi same thing goes for *Wn,*W_e,*W_o