Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a C function that takes three arguments: a character and two integers.

ID: 3758828 • Letter: 1

Question

1. Write a C function that takes three arguments: a character and two integers. The character is to be printed. The first integer specifies the number of times that the character is to be printed on a line, and the second integer specifies the number of lines that are to be printed. Write a program that makes use of this function. Call the program charblock.c

2. Write and test a C function that takes the addresses of three double variables as arguments and that moves the value of the smallest variable into the first variable, the middle value to the second variable, and the largest value into the third variable. Write a program that reads in 3 double values, and then prints the values in sorted order using the function you just created. Call the program dsort.c

Explanation / Answer

1)

#include <stdio.h>
void Print_line (char ch, int numberOfChars, int numberOfLines);

int main (void)
{
    char userChar;
    int lines,times;
    printf("please enter a character, number of times in a line, and number of lines, separated by a comma: ");
    while ((scanf("%c,%d,%d", &userChar, &times, &lines)) == 3)
    {
         Print_line (userChar, times, lines);
    }
    return 0;
}

void Print_line (char ch, int numberOfChars, int numberOfLines)
{
    int x;
    int y = 0;

    while (++y <= numberOfLines)
    {
            for (x = 0; x<numberOfChars; x++)
            {
                printf ("%c", ch);
            }
            printf (" ");
    }
}

2)

#include <stdio.h>

//Always use meaningful names for types
typedef unsigned char boolean;
#define True 't'
#define FALSE (!True)

void swap(long* a,long *b)
{
*a=*a^*b;
*b=*b^*a;
*a=*a^*b;
}

int main(int n, char*args[]){
double *d;
int i;
char input[5];
boolean sorted = FALSE;


printf("Please enter the length of the array ");
gets(input);

sscanf(input,"%s",&input[0]);
n=(long)atol(input);


d = calloc(sizeof(double),n);

//Get and sort the array
until (sorted) {

     for (i=0;i<n;i++) {
    
        printf("Please enter the %d%s array item ",i,i==1?"st":"th");
        scanf("%lf",d+i);
     }
     //do a compare and exchange sort:
     sorted = !sorted; //not sorted
   
     //check all the items
     printf("%d %d ",i,n);
     for (i=1;i<n;i++)
     {
        //compare
        if (d[i]<d[i-1])
        {
          //exchange
          swap(d+i,d+i-1);
          sorted = FALSE;
        }
     }
     //show results
     printf("The array is%ssorted ",sorted?" ":" not ");
     }
for (;n-->0;) printf("%lf ",*d++);
}