1> the square root of a positive n can be obtained by calculating a series of approximations, each of which is closer to the exact value than the previous one. The first approximation is 1, and the next approximation is obtained by the following formula. Write a program, read in a value, calculate and print its square root.
Int main () { float a = 1 ; float b = 0; float n = 0; printf ("Please enter: \ n"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%f", &n); if (n <= 0) { printf ("Enter the wrong number!") "); } else { do { b = a; a = (b + n / b) / 2; printf ("%f\n", a); } while (B != a); printf (" The square root is:%f\n ", a); } system (" Pause "); return 0;}
2. If an integer is divisible only by itself and 1, it is called Prime, write a program that prints all the prime numbers between 1-100
Int main () { int i = 0; int j = 0; int k = 0; for (i = 3; i <= 100; i+=2)//Even number cannot be prime number { k = (int) sqrt (i); for (j = 2; j <= k; j++)//Note the range of J, can improve the computational efficiency { if (i%j == 0) break; } if (i%j != 0) priNTF ("%d\n", i); } system ("pause"); return 0 ;}
3. The length of the three edges of the equilateral triangle is equal, but only two of the isosceles triangle are equal. If the three sides of the triangle are unequal in length, it is called equilateral triangle
Write a program that prompts the user to enter three numbers, representing the length of the triangle three edges, and then the program determines what type of triangle it is.
Hint: Is there something else that the program should consider in addition to the length of the edges?
Int main () { int a = 0; int b = 0; int c = 0; printf ("Please enter: \ n"); &NBSP;&NBSP;SCANF ("%d%d%d", &a, &b, &c); if ((a + &NBSP;B&NBSP;>&NBSP;C&&A&NBSP;-&NBSP;B<C) | | (a + c>b&&a - c<b) | | (B + c>a&&b - c < a) { if ((a == b) | | (a == c) | | (B&NBSP;==&NBSP;C)) printf ( "This is a isosceles triangle \ \"); if (a == b) & & (b == c) printf ("This isa equilateral triangle \ \ "); else printf ("This is a not equilateral triangle \ n"); } else { printf ("These three edges cannot form a triangle!") \ n "); } system (" pause "); return 0;}
4. Write the function Copy_n, its function prototype is as follows: void Copy_n (char dst[],char src[],int N);
This function is used to copy a string from the array src to DST, but there are the following requirements: You must copy the n characters to DST exactly, not more, or less. If the length of the SRC string is less than n, you must add enough nul characters at the end of the copied
string so that it is exactly the length of N. If the SRC length is longer than or equal to N, then you can stop it by storing n characters in DST. At this point, the array DST will not end with NUL.
Note that when calling Copy_n, it should store something in the space of dst[0] to dst[n-1], but it is limited to those locations, regardless of the length of the SRC.
Void copy_n (char dst[], char src[], int n) { int n_ copyed = 0;//number of bytes copied while (*src != ')//Note *src { if (n_copyed == n)//n<sizeof ( SRC) { break; } *dst++ = *src++; n_copyed++; } if (n_copyed < n)//n>sizeof (SRC) { int i = 0; for (i = 0; i < (n - n_copyed); i++) { *dst = ' '; dst++; } }}int main () { char dst[10] = { 0 }; char src[10] = { "ASDF" }; int n = 3; copy_n (dst, src, n); printf (DST); system ("pause"); return 0;}
5. Write a program that reads text from a line of standard input and accomplishes the following tasks: if there are two or more lines adjacent to the same text in the file, then one line is printed, and the remaining lines are not printed.
You can assume that the lines of text in a file will not exceed 128 characters in length.
Tip: Use the Get function to read input rows, use strcpy to copy them, strcmp to compare them, equal functions to return 0, not equal to return non-0 values
This is the first lineanother lineand anotherand anotherand anotherand anotherstill morealmost do now--almost done now-- Another Linestill morefinished
#define &NBSP;MAX_LINE&NBSP;20&NBSP;//Maximum number of processing rows #define max_cols 128//Maximum processing columns//processing input row data Void range (char input[max_line][max_cols], char output[max_line], int i) { int flag =0; while (flag <= i) { if (strcmp (Input[flag), &NBSP;INPUT[FLAG&NBSP;+&NBSP;1] == 0) { strcpy (Output, input[flag]); if (strcmp (output, input[flag + 2]) != 0) printf ("%s\n", output); } flag++; }}int main () { char input[MAX_LINE][MAX_COLS] = { 0 }; //to store input rows char output[max_line] = { 0 }; //Storage Output Line int i=0; //input line into input array with CTRL + Z end input while (get (Input[i]) != null) &NBSP;&NBSP;&NBSP;&NBsp; { i++; } range (input, output, i);//Call the range function system ("pause"); return 0;}
6. Write a function that extracts a string from a string. The prototype of the function should be as follows:
int substr (char dst[],char src[],int start,int len);
The task of the function is to start at the beginning of the SRC array, offset from the start position, and copy len non-null characters to the DST array up to a maximum. After the copy is complete, the DST array must end with a null byte
The return value of the function is the length of the string stored in the DST array.
If the position specified by start crosses the tail of the SRC array, or the value of start or Len is negative, then the copy to the DST array is an empty string.
#include <assert.h>int substr (char dst[], char src[], int start , int len) { assert (DST); assert (SRC); The char *p = src + start;//pointer variable points to the address to extract the string from int i = 0; int n = strlen (P);// The length of the string after start if (N < len) len = n; while (len) { dst[i] = src[start-1]; i++; len--; start++; } dst[i + 1] = ' + '; i&Nbsp;= i + 1; return i;} Int main () { char src[20] = "Heloworld"; char dst[20] = { 0 }; int len = 6; int start = 1; substr (DST, src, start, len); printf ("%s", &NBSP;DST); system ("pause"); return 0;}
7. Write a function to remove the extra spaces from a string. Function Prototypes:
void Deblank (char string[])
When a function finds a place in a string that consists of one or more contiguous spaces, it changes to a single space character. Note that when you traverse the entire string you want to make sure that it ends with a nul character
Void deblank (char string[]) { int i = 0; int len = 0; while (1) { string[i] = getchar (); if (string[i] == ' \ n ') break; i+ +; } len = i; for (i = 0; i < len; i++) { if (string[i] == ' ' && string[i - 1] == ' ') continue; &nbsP; putchar (String[i]); }}int Main () { char c[120]; printf ("Please enter: \ n"); deblank (c); system ("Pause"); return 0;}
This article is from the "Sean" blog, so be sure to keep this source http://seann.blog.51cto.com/11126490/1740215
C and Pointer fourth chapter programming exercises