Publicité

--- Rewrite checkSorted() and replace() using good coding style- --- U.docx

20 Feb 2023
--- Rewrite checkSorted() and replace() using good coding style- --- U.docx
--- Rewrite checkSorted() and replace() using good coding style- --- U.docx
--- Rewrite checkSorted() and replace() using good coding style- --- U.docx
--- Rewrite checkSorted() and replace() using good coding style- --- U.docx
Prochain SlideShare
Still stuck with some errors here- Someone said my code had issues wit.docxStill stuck with some errors here- Someone said my code had issues wit.docx
Chargement dans ... 3
1 sur 4
Publicité

Contenu connexe

Plus de hoggardbennie(20)

Publicité

--- Rewrite checkSorted() and replace() using good coding style- --- U.docx

  1. --> Rewrite checkSorted() and replace() using good coding style. --> Updated the main() function if/as needed. --> Write comments to briefly explain your improvements. Written by: */ #include <stdio.h> #include <string.h> #include <ctype.h> struct stu{ long id; char name[32]; }; int checkSorted(char password[]); int replace(struct stu list[], int *length, const struct stu *ptrData, int *location); int main( ) { // Test checkSorted() char myPassword[] = "abcdefg"; char oldPassword[] = "bcdeabc"; printf("%s %dn", myPassword, checkSorted(myPassword)); printf("%s %dnn", oldPassword, checkSorted(oldPassword)); // Test replace() struct stu list[10] =
  2. { {88219017, "Ron House"}, {34951406, "John W. Perry"}, {31103628, "Dennis M. Ritchie"} }; int length = 3; int status; int location = 2; struct stu newData = {11223344, "Brian W. Kernighan"}; for (int i = 0; i < length; i++){ printf("%ld %sn", list[i].id, list[i].name); } status = replace(list, &length, &newData, &location); printf("status: %dn", status); for (int i = 0; i < length; i++){ printf("%ld %sn", list[i].id, list[i].name); } return 0; } /* This function checks if the characters in pasword are sorted in ascending order Rewrite the following code to: --> eliminate the call for strlen()
  3. --> replace index k with a pointer --> make other changes to improve the code --> write comments to briefly explain your improvements */ int checkSorted(char password[]) { int k; int sorted = 1; k = 1; while (sorted == 1 && k < strlen(password)) { if (password[k] < password[k - 1]) sorted = 0; k++; } if (sorted == 1) return 1; else if (sorted == 0) return 0; else return 2; } /* This function replaces the element at a given location in list[]
  4. with data pointed to by its third parameter. What are the changes that would improve this function? Make it: --> simpler --> shorter --> easier to read Write comments to briefly explain your improvements. */ int replace(struct stu list[], int *length, const struct stu *ptrData, int *location) { if (*location < 0 || *location >= *length){ return 0; // location is out of range } list[*location].id = ptrData->id; strcpy(list[*location].name, ptrData->name); return 1; }
Publicité