0% found this document useful (0 votes)
10 views3 pages

Assign 7

The document contains several C programming tasks that involve writing functions for various mathematical and string operations. These include summing prime numbers in a list, calculating the sum of factors and digits of a number, counting words in a string, finding Hamming distance between two integers, and evaluating polynomials. Each task is accompanied by example code and expected output.

Uploaded by

Kr REVANKAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Assign 7

The document contains several C programming tasks that involve writing functions for various mathematical and string operations. These include summing prime numbers in a list, calculating the sum of factors and digits of a number, counting words in a string, finding Hamming distance between two integers, and evaluating polynomials. Each task is accompanied by example code and expected output.

Uploaded by

Kr REVANKAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write a function which takes a list of integers and its length and returns the
sum of all the prime numbers in the list.

#include <stdio.h>
#include <stdbool.h>

// Function to check if a number is prime


bool is_prime(int n) {
if (n <= 1) return false; // 0 and 1 are not prime
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false; // If divisible, not prime
}
return true;
}

// Function to sum all prime numbers in a list


int sum_primes(int *arr, int length) {
int sum = 0;
for (int i = 0; i < length; i++) {
if (is_prime(arr[i])) {
sum += arr[i];
}
}
return sum;
}

int main() {
int x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int s = sum_primes(x, 10);
printf("Sum of primes: %d\n", s); // Output: 17
return 0;
}

-----------------------------------------------------------------------------------
-----------------

2. Write a function which takes an unsigned integer and returns the sum of all the
factors of the number.

#include <stdio.h>

// Function to sum all factors of a number


int sum_factors(unsigned int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum;
}

int main() {
int s = sum_factors(10);
printf("Sum of factors: %d\n", s); // Output: 18
return 0;
}
3. Write a function which takes an unsigned integer and returns the sum of all the
digits in the number.

#include <stdio.h>

// Function to sum all digits of a number


int sum_digits(unsigned int n) {
int sum = 0;
while (n > 0) {
sum += n % 10; // Extract the last digit
n /= 10; // Remove the last digit
}
return sum;
}

int main() {
int s = sum_digits(1278);
printf("Sum of digits: %d\n", s); // Output: 18
return 0;
}

4. Write a function which takes a string and returns the number of words in the
string. Assume
that there is always one space separating the words and there are no spaces at the
begining
or the end of the string.

#include <stdio.h>

// Function to count words in a string


int count_words(char *str) {
if (str[0] == '\0') return 0; // Empty string
int count = 1; // At least one word
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ' ') {
count++; // Increment count for each space
}
}
return count;
}

int main() {
int s = count_words("hello world, how are you?");
printf("Number of words: %d\n", s); // Output: 5
return 0;
}

-----------------------------------------------------------------------------------
--------------

5. Write a function to find the hamming distance between the bit representation of
two unsigned
integers. Hamming distance is the number of places the two bit representations
differ. e.g.

#include <stdio.h>

// Function to calculate Hamming distance


int hamming_distance(unsigned int a, unsigned int b) {
int distance = 0;
unsigned int xor_result = a ^ b; // XOR to find differing bits
while (xor_result > 0) {
distance += xor_result & 1; // Check if the last bit is 1
xor_result >>= 1; // Shift right to check the next bit
}
return distance;
}

int main() {
printf("Hamming distance: %d\n", hamming_distance(7, 9)); // Output: 3
return 0;
}

-----------------------------------------------------------------------------------
----------------

6. Write a function that evaluates a quadratic polynomial at a point. The arguments


to the
function are an array of 3 integers (coefficients of the polynomial), and the point
where the
polynomial is to be evaluated.

float coefficients[] = {2.0, -1.0, -3.2}; // Polynomial: -3.2*x^2 - 1.0*x + 2.0


result = evaluate_quad(coefficients, 3.5);
result == -40.7 // You may get slightly different results because of floating
point inaccuracy

7. This is a generalization of problem 6.


Write a function that evaluates the value of a polynomial at a point. The arguments
to the
function are the degree of the polynomial, an array with coefficients and the point
to evaluate at.
float coefficients[] = {2.0, 1.0, -1.5, 0.75}; // Polynomial: 0.75*x^3 - 1.5*x^2 +
1.0*x + 2.0
int degree = 3;
result = eval_poly(degree, coefficients, 2.0);
result == 4.0

You might also like