Classes/csci218/Lect/108.c

182 lines
3.8 KiB
C

#include <stdio.h>
#include <ctype.h>
#include <string.h>
void ex1();
void ex2();
void ex3();
void ex4();
void ex5();
void ex6();
void ex7();
void ex8();
int main()
{
ex1();
ex2();
ex3();
ex4();
ex5();
ex6();
ex7();
ex8();
return 0;
}
void ex1(){
printf("\nThis program count how many negative numbers exist in the array.\n");
int nums[] = { 3, -2, 1, -1, 8, 9, -4};
//declare a variable to accumulate negative numbers
int numNeg = 0;
//calculat the # of elements of array
int len = sizeof(nums)/sizeof(nums[0]);
//iterate arrays
for (int i = 0; i < len; i++) {
if (nums[i] < 0) {
numNeg++;
}
}
printf("There are %d negatives in the array\n", numNeg);
}
void ex2(){
printf("\nThis program count how many odds exist in the array\n");
int nums[] = { 3, -2, 1, -1, 8, 9, -4};
//declare a variable to accumulate # of odd numbers
int numOdd = 0;
//calculat the # of elements of array
int len = sizeof(nums)/sizeof(nums[0]);
//iterate arrays
for (int i = 0; i < len; i++) {
if (nums[i] % 2) {
numOdd++;
}
}
printf("There are %d odds in the array\n", numOdd);
}
void ex3(){
printf("\nThis program replace the negative numbers as 0 in the array\n");
int nums[] = { 3, -2, 1, -1, 8, 9, -4};
int len = sizeof(nums)/sizeof(nums[0]);
//iterate arrays and
for (int i = 0; i < len; i++ ){
if (nums[i] < 0) {
nums[i] = 0;
}
}
//display the aupdated array
printf("The array looks like:");
for (int i = 0; i < len; i++) {
printf(" %d", nums[i]);
}
printf("\n");
}
void ex4(){
printf("\nThis program will take an input string and determine the number of alphas\n");
//Count how many alphabet letters in a string
printf("Please enter a string no more than 50 characters: ");
char var[50];
//get the string from the user
scanf("%s", var);
//declare a variable to accumulate # of alphabet
int numAlpha = 0;
//calculat the # of elements of the string
int len = sizeof(var)/sizeof(var[0]);
//iterate the string
for (int i = 0; i < len; i++) {
if (isalpha(var[i])) {
numAlpha++;
}
}
printf("There are %d alphabet in the string\n", numAlpha);
}
void ex5(){
printf("This program ask for a string, then convert all alphabet as uppercase.\n");
char str[51];
printf("Please enter a string no more than 50 characters: ");
scanf("%s", str);
for (int i = 0; i < strlen(str); i++) {
if (isalpha(str[i])) {
str[i] = toupper(str[i]);
}
}
printf("Modified String: %s\n", str);
}
void ex6(){
printf("This program concatenate a string to another.\n");
char str1[50] = "Hello! ";
char str2[50] = "csci218";
strcat(str1, str2);
printf("Concated string: %s\n", str1);
}
void ex7(){
printf("This program find the maximum of the array.\n");
int nums[] = { 3, -2, 1, -1, 8, 9, -4};
//declare a variable the hold the maximum found so far
int max = nums[0];
int len = sizeof(nums)/sizeof(nums[0]);
//iterate the array and update the maximum
for (int i = 1; i < len; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
printf("the maximum is %d\n", max);
}
void ex8(){
printf("This program find the maximum of the array and its index.\n");
int nums[] = { 3, -2, 1, -1, 8, 9, -4};
//declare a variable the hold the maximum found so far
int max = nums[0];
int maxI = 0;
int len = sizeof(nums)/sizeof(nums[0]);
//iterate the array and update the maximum
for (int i = 1; i < len; i++) {
if (nums[i] > max) {
max = nums[i];
maxI = i;
}
}
//iterate the array and update the maximum
printf("the maximum is %d, the index is %d\n", max , maxI);
}