Question

A)Correct this code and explain what was corrected // C code - For Loop / Array...

A)Correct this code and explain what was corrected

// C code - For Loop / Array Population

// This program will populate integers into an array, and then display those integers.

// Developer: Johnson, William CMIS102 // Date: Mar 4, 2020

// Global constants

#define INTLIMIT 10

#include <stdio.h>

// This function will handle printing my name, class/section number, and the date

void printStudentData()

{

char fullName[19] = "William J. Johnson";

char classNumber[9] = "CMIS 102";

char sectionNumber[5] = "4020";

char currentDate[22] = "March 4th, 2020";

printf("Name: \t %s \n", fullName);

printf("Class: \t %s \n", classNumber);

printf("Section: %s \n", sectionNumber);

printf("Date: \t %s \n\n", currentDate); }

// Declare 'global' variables; available to all functions except printStudentData() int intArray[INTLIMIT], i;

// This function will populate the array with integers void populateArray()

{

for (i=0;i<INTLIMIT;;I--)

 intArray[i] = i*i;
}

// This function will display the integers in the array
void displayArray()
{
  for (i=0;i<INTLIMIT;i--)
  {
    printf("Number %d: \t", i+1);
    printf("%d\n", intArray[10-i]);
  }
}
// Start of program
int main(void) {
  printStudentData();
  displayArray();
  populateArray();
printf("\n\nThis program executed succesfully!\n\n");
return 0;
}//End main
  printf("\n\nThis program executed successfully!\n\n");
  return 0;
} // End main
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Corrections made in the given code to make in run successfully:

  1. In the for loop inside populateArray() function remove extra semicolon after i<INTLIMIT and change control variable to lower case 'i'. Also increment the control variable (i++)
  2. In the for loop inside displayArray() function, increment the control variable (i++)
  3. Inside the for loop inside displayArray() function, display each element in the array using index directly, no need to subtract index from 10 (printf("%d\n", intArray[i]);)
  4. Inside the main() function invoke populateArray() function first and then displayArray() function. Because you need to populate the array with values before displaying it
  5. Inside the main() function remove the following redundant lines
printf("\n\nThis program executed successfully!\n\n");
  return 0;
} // End main

Given below is the program after making correction:

#define INTLIMIT 10

#include <stdio.h>

// This function will handle printing my name, class/section number, and the date

void printStudentData()

{

char fullName[19] = "William J. Johnson";

char classNumber[9] = "CMIS 102";

char sectionNumber[5] = "4020";

char currentDate[22] = "March 4th, 2020";

printf("Name: \t %s \n", fullName);

printf("Class: \t %s \n", classNumber);

printf("Section: %s \n", sectionNumber);

printf("Date: \t %s \n\n", currentDate);
  
}

// Declare 'global' variables; available to all functions except printStudentData()
int intArray[INTLIMIT], i;

// This function will populate the array with integers

void populateArray()
{
for (i=0;i<INTLIMIT;i++) {
intArray[i] = i*i;
}
}

// This function will display the integers in the array
void displayArray()
{
for (i=0;i<INTLIMIT;i++)
{
printf("Number %d: \t", i+1);
printf("%d\n", intArray[i]);
}
}
// Start of program
int main(void) {
printStudentData();
populateArray();
displayArray();

  
printf("\n\nThis program executed succesfully!\n\n");
return 0;
} // End main

Screenshot of code and output from an IDE

***************

CODE

***************

***************

OUTPUT

***************

Add a comment
Know the answer?
Add Answer to:
A)Correct this code and explain what was corrected // C code - For Loop / Array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • How would you correct this function in C to prevent buffer overflow? void nameBuilder() {   ...

    How would you correct this function in C to prevent buffer overflow? void nameBuilder() {    char fname[10];    char lname[10];    char fullname[20];    printf("Enter your first name: ");    scanf("%s", fname);    printf("Enter your last name: ");    scanf("%s", lname);    strcat(fullname, fname);    strcat(fullname, " ");    strcat(fullname, lname);    printf("Welcome. %s\n", fullname);    return; }

  • C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char...

    C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char firstname[16]; char lastname[16]; printf("please enter your first name:"); scanf("%s",firstname); printf("please enter your last name:"); scanf("%s",lastname); PrintName(firstname,lastname); return 0; } void PrintName(char firstname[16], char lastname[16]){ char fullname[34]; *fullname=*firstname+*lastname; (char*) malloc (sizeof (*fullname)); if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16) fflush(stdin); else printf(" the full name is %s %s \n",firstname,lastname); printf(" the full name is-> %s",fullname);/*why is one not run*/ return 0; }

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • this is c code. please answer all questions on a piece of paper and show work....

    this is c code. please answer all questions on a piece of paper and show work. i need to prepare as i have a midterm i will have to be completing on paper 1) Bit Operators: This C program compiles and runs. What is its output? 1) #include <stdio.h> 2) void main (void) 3) unsigned char x =60; 4) 5) 6) 7) 8 ) 9) 10) 11) 12) 13) unsigned char a = x < 1; unsigned char b unsigned...

  • If void * is a pointer to void is a "generic" pointer type, and a void...

    If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • please help with the operator overloading lab (intArray) in c++ will provide what it is being...

    please help with the operator overloading lab (intArray) in c++ will provide what it is being required and the code that was given from the book. the code that was provided is below ------------------------------------------------------------------------------------------------------------------------- // iadrv.h #ifndef _IADRV_H #define _IADRV_H #include "intarray.h" int main(); void test1(); void test2(); void test3(); void test4(); void test5(); void test6(); void test7(); void test8(); void test9(); void test10(); void test11(); void test12(); void test13(); void test14(); void test15(); void test16(); void test17(); void test18();...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT