Array Questions:
Q: Write a program to enter price of 3 items and print their final cost with GST?
#include <stdio.h>
int main(){
float items[3];
printf("Enter three prices : ");
scanf("%f", &items[0]);
scanf("%f", &items[1]);
scanf("%f", &items[2]);
printf("Final cost of item 1 is %f\n ", items[0] + (0.18 * items[0]));
printf("Final cost of item 2 is %f\n ", items[1] + (0.18 * items[1]));
printf("Final cost of item 3 is %f\n ", items[2] + (0.18 * items[2]));
return 0;
}
Q: Write a function to count the number of odd numbers in an array?
#include <stdio.h>
int oddCount(int arr[], int size);
int main(){
int size;
printf("Enter the size of array : ");
scanf("%d", &size);
int number[size];
printf("Enter the values of array\n");
for(int i=0; i<size; i++){
scanf("%d", &number[i]);
}
printf("Count of odd numbers : %d", oddCount(number, size));
return 0;
}
int oddCount(int arr[], int size){
int count = 0;
for(int i=0; i<size; i++){
if(arr[i] % 2 != 0){
count += 1;
}
}
return count;
}
Q: Create an array of 10 numbers. Verify using pointer arithmetic that (ptr+2) points to the third element where ptr is a pointer pointing to the first element of the array.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7};
int* ptr = arr;
printf("The value at address %u is %d", ptr+2, *(ptr+2));
return 0;
}
Q: If S[3] is a 1-D array of integers then *(S+3) refers to the third element:
(i) True.
(ii) False.
(iii) Depends.
The correct answer is (ii) False.
Explanation:
If S is a 1-D array of integers (e.g., int S[3]), the expression *(S + 3) does not refer to the third element; it attempts to access the fourth element because array indexing in C is zero-based.
- S is the base address of the array.
- S + 3 points to the memory location 3 elements away from the base address.
- *(S + 3) dereferences this pointer to access the value at the calculated address.
Q: Write a program containing a function which reverses the array passed to it?
#include <stdio.h>
void arr_reverse(int[], int);
int main() {
int arr[] = {1,2,3,4,5};
int n = sizeof(arr)/4;
printf("Original array: ");
for(int i=0; i<n; i++){
printf("%d ", arr[i]);
}
arr_reverse(arr, n);
return 0;
}
void arr_reverse(int arr[], int n) {
int temp;
// int start = 0, end = n-1;
// for(int i=0; i<n/2; i++) {
// temp = arr[start];
// arr[start] = arr[end];
// arr[end] = temp;
// start++;
// end--;
// }
// The same functionality with slightly less overhead.
for (int i = 0; i < n / 2; i++) {
temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
printf("\nReversed array: ");
for(int i=0; i<n; i++){
printf("%d ", arr[i]);
}
}
Q: Write a program to store the first n fibonacci numbers?
#include <stdio.h>
void fibo(int arr[], int n);
int main(){
int n;
printf("Enter the number : ");
scanf("%d", &n);
int arrfibo[n];
fibo(arrfibo, n);
return 0;
}
void fibo(int arr[], int n){
arr[0] = 0;
arr[1] = 1;
if(n == 1){
printf("%d", arr[0]);
return;
}
if(n == 2){
printf("%d, %d", arr[0], arr[1]);
return;
}
printf("%d, %d, ", arr[0], arr[1]);
for(int i=2; i<n; i++){
arr[i] = arr[i-2] + arr[i-1];
printf("%d, ", arr[i]);
}
}
Q: Write a program to create an array of 10 integers and store multiplication table of 5 in it.
#include <stdio.h>
int main() {
int table_5[10];
for(int i=0; i<10; i++){
table_5[i] = (1+i)*5;
}
for(int i=0; i<10; i++){
printf("5 x %d = %d\n", i+1, table_5[i]);
}
return 0;
}
Q: Repeat above problem for a general input provided by the user using scanf.
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int table_5[10];
for(int i=0; i<10; i++){
table_5[i] = (1+i) * n;
}
for(int i=0; i<10; i++){
printf("%d x %d = %d\n", n, i+1, table_5[i]);
}
return 0;
}
Q: Create a 2D array, storing the table of 2 and 3.
#include <stdio.h>
int main(){
int table[10][2];
printf("Table of 2 and 3\n");
for(int i=0; i<10; i++){
table[i][0] = 2 * (i+1);
table[i][1] = 3 * (i+1);
}
for(int i=0; i<10; i++){
printf("2 * %d = %d\t", i+1, table[i][0]);
printf("3 * %d = %d\n", i+1, table[i][1]);
}
return 0;
}
Q: Create a three–dimensional array and print the address of its elements in increasing order.
#include <stdio.h>
int main() {
int arr[1][2][3];
for(int i=0; i<1; i++) {
for(int j=0; j<2; j++) {
for(int k=0; k<3; k++) {
printf("%u\n", &arr[i][j][k]);
}
}
}
return 0;
}
Q: In an array of numbers, find how many times does a number 'x' occurs.
#include <stdio.h>
int main() {
int size, i, x, count = 0;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
// Input the elements of the array
printf("Enter %d elements: ", size);
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Input the number to count
printf("Enter the number to count: ");
scanf("%d", &x);
// Count the occurrences of x
for (i = 0; i < size; i++) {
if (arr[i] == x) {
count++;
}
}
// Output the result
printf("%d occurs %d times in the array.\n", x, count);
return 0;
}
Q: Write a program to print the largest number in an array.
#include <stdio.h>
int main() {
int size, i, max;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
// Input the elements of the array
printf("Enter %d elements\n", size);
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Find the maximun number in an array
max = arr[0];
for (i = 1; i < size; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
// Output the result
printf("Largest number in an array is %d", max);
return 0;
}
Q: Write a program containing functions which counts the number of positive integers in an array.
#include <stdio.h>
int count_positive_int(int[], int);
int main() {
int arr[] = {1,-2,3,4,-5};
int n = sizeof(arr)/4;
printf("The no. of positive integer is %d", count_positive_int(arr, n));
return 0;
}
int count_positive_int(int arr[], int n) {
int count = 0;
for(int i=0; i<n; i++){
if(arr[i] > 0) {
count++;
}
}
return count;
}
Q: Write a program to insert an element at the end of an array.
#include <stdio.h>
int main() {
int size, i, value;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
// Create an array with one extra space for the new element
int arr[size + 1];
// Input the elements of the array
printf("Enter %d elements\n", size);
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Ask the user for the new element to insert
printf("Enter the element to insert at the end: ");
scanf("%d", &value);
// Insert the new element at the end
arr[size] = value;
// Print the updated array
printf("Updated array\n");
for (i = 0; i <= size; i++) {
printf("%d\t", arr[i]);
}
return 0;
}
Q: Ask the user to enter their firstName and print it back to them & Also try this with their fullName.
#include <stdio.h>
int main() {
// char firstName[50];
// printf("Enter your name: ");
// scanf("%s", firstName);
// printf("Your name is : %s\n", firstName);
char fullName[100];
printf("Enter your Full name: ");
// scanf("%s", fullName); // takes only first name. It does not count speces
// Alternative
// gets(fullName); // Input -> Dengerous/unsafe or outdated
fgets(fullName, 100, stdin); // Input
printf("Your Full name is : ");
puts(fullName); // Output
return 0;
}
Q: Make a program that inputs user's name and prints its length.
#include <stdio.h>
#include <string.h>
void printName(char arr[]);
int nameLength(char arr[]);
int main() {
char name[100];
printf("Enter your name: ");
fgets(name, 100, stdin);
printName(name);
printf("%d", nameLength(name));
return 0;
}
void printName(char arr[]){
// puts(arr);
printf("%s", arr);
}
int nameLength(char arr[]){
// int count = 0;
// for(int i=0; arr[i] != '\0'; i++){
// count++;
// }
// return count-1;
// OR
int length = strlen(arr);
return length;
}
Q: Find the salting form of a password entered by user if the salt is "123" and added at the end.
#include <stdio.h>
#include <string.h>
void salting(char psd[]);
int main() {
char psd[30];
salting(psd);
return 0;
}
void salting(char psd[]){
char salt[] = "123";
char newPsd[40];
printf("Enter password : ");
scanf("%s", psd);
strcpy(newPsd, psd); // newPsd = psd
strcat(newPsd, salt); // newPsd + salt
// puts(newPsd);
printf("New password is : %s", newPsd);
}
Q: Write a function named slice, which takes a string and returns a sliced string from index n to m?
#include <stdio.h>
#include <string.h>
void userSlice(char str[], int startIndex, int endIndex);
int main() {
char str[] = "hello World";
int startIndex = 1;
// int endIndex = strlen(str);
int endIndex = 6;
userSlice(str, startIndex, endIndex);
return 0;
}
void userSlice(char str[], int startIndex, int endIndex){
char newStr[m-n+1];
int ind = 0;
for(int i=startIndex; i<=endIndex; i++, ind++){
newStr[ind] = str[i];
}
newStr[ind] = '\0';
puts(newStr);
}
Q: Write a function to count the occurrence of vowels in a string?
#include <stdio.h>
int countVowel(char str[]);
int main() {
char str[] = "hello World";
printf("%d", countVowel(str));
return 0;
}
int countVowel(char str[]){
int count = 0;
for(int i=0; str[i] != '\0'; i++){
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u'){
count++;
}
}
return count;
}
Q: Write a program to convert all lowercase vowels to uppercase in a string?
#include <stdio.h>
#include <string.h>
void countVowel(char str[]);
// char countVowel(char str[]);
int main() {
char str[] = "hello World";
countVowel(str);
// printf("%d", countVowel(str));
return 0;
}
void countVowel(char str[]){
int count = 0;
for(int i=0; str[i] != '\0'; i++){
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u'){
str[i] -= 32;
}
printf("%c", str[i]);
}
}
// char countVowel(char str[]){
// int count = 0;
// for(int i=0; str[i] != '\0'; i++){
// if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u'){
// str[i] -= 32;
// }
// printf("%c", str[i]);
// }
// return puts(str);
// }
Q: Write a program to print the highest frequency character in a string?
Q: Write a program to remove black spaces in a string?
#include <stdio.h>
#include <string.h>
void removeWhiteSpeces(char str[]);
int main() {
char str[] = "hello World hello World hello World world";
printf("%s\n", str);
removeWhiteSpeces(str);
return 0;
}
void removeWhiteSpeces(char str[]){
for(int i=0; str[i] != '\0'; i++){
if(str[i] == ' '){
continue;
}
printf("%c", str[i]);
}
}
Q: Write a program to replace lowercase letters with uppercase and vice versa in a string?
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello World";
for(int i=0; str[i] != '\0'; i++){
if(isupper(str[i])){
str[i] = tolower(str[i]);
}
else {
str[i] = toupper(str[i]);
}
}
puts(str);
return 0;
}
0 Comments