0% found this document useful (0 votes)
59 views

AFP - Automação. Programming Marathon: Challenge 1

This document presents solutions to 11 programming exercises pertaining to a programming marathon challenge. Each solution includes a software requirements statement, a flowchart of the solution, and C code with comments to implement the solution. The exercises include programs for converting between rectangular and polar coordinates for complex numbers, printing a message from a function, calculating the sum of two integers, determining if a number is even or odd, finding all divisors of a number, a basic calculator, finding the maximum of three numbers, converting between dollars and Bangladeshi Taka, finding the absolute value of a number, calculating the area of basic shapes, and converting a string to uppercase.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

AFP - Automação. Programming Marathon: Challenge 1

This document presents solutions to 11 programming exercises pertaining to a programming marathon challenge. Each solution includes a software requirements statement, a flowchart of the solution, and C code with comments to implement the solution. The exercises include programs for converting between rectangular and polar coordinates for complex numbers, printing a message from a function, calculating the sum of two integers, determining if a number is even or odd, finding all divisors of a number, a basic calculator, finding the maximum of three numbers, converting between dollars and Bangladeshi Taka, finding the absolute value of a number, calculating the area of basic shapes, and converting a string to uppercase.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Rocket Team

SOFTWARE DELIVERY

AFP – Automação.
Programming Marathon
Challenge 1

Reference: Version ,
Version No: 1.0
Status: Draft
Prime Author: Enzo Pirineli Pacheco
Group: Rocket Team
Document No: 000001

Prepared by: To be reviewed by: To be signed off by:

Reference: Version ,
Andersen Consulting Service Delivery

TABLE OF CONTENTS

1. Introduction .............................................................................................................................................. 2

2. Exercise 1 – Conversor. ........................................................................................................................... 3


2.1 Flowchart: ......................................................................................................................................... 3
2.2 Code: .................................................................................................................................................. 3

3. Exercise 2 – Hello inside the function. .................................................................................................. 5


3.1 Flowchart: ......................................................................................................................................... 5
3.2 Code: .................................................................................................................................................. 5

4. Exercise 3 – Sum of two integers. .......................................................................................................... 6


4.1 Flowchart: ......................................................................................................................................... 7
4.2 Code: .................................................................................................................................................. 7

5. Exercise 4 – EVEN or ODD. ................................................................................................................... 8


5.1 Flowchart: ......................................................................................................................................... 8
5.2 Code: .................................................................................................................................................. 8

6. Exercise 5 - All of divisors. ..................................................................................................................... 9


6.1 Flowchart: ......................................................................................................................................... 9
6.2 Code: ................................................................................................................................................ 10

7. Exercise 6 – Calculator .......................................................................................................................... 10


7.1 Flowchart: ....................................................................................................................................... 11
7.2 Code: ................................................................................................................................................ 11

8. Exercise 7 – findMax. ............................................................................................................................ 12


8.1 Flowchart: ....................................................................................................................................... 13
8.2 Code: ................................................................................................................................................ 13

9. Exercise 8 – Dollar to BDT. ................................................................................................................... 14


9.1 Flowchart: ....................................................................................................................................... 14
9.2 Code: ................................................................................................................................................ 15

10. Exercise 9 – Absolute value. ............................................................................................................. 15


10.1 Flowchart: ....................................................................................................................................... 16
10.2 Code: ................................................................................................................................................ 16

11. Exercise 10 – Area. ............................................................................................................................. 17


11.1 Flowchart: ....................................................................................................................................... 17
11.2 Code: ................................................................................................................................................ 17

12. Exercise 11 – Upper Case. ................................................................................................................. 18


12.1 Flowchart: ....................................................................................................................................... 19
12.2 Code: ................................................................................................................................................ 19

Reference: Version ,
Andersen Consulting Service Delivery

1. Introduction

That document presents the solutions for 10 exercises pertaining to challenge 1 of the programming
:
marathon ... Each solution will present:
- Software requirements -> statement of the exercise.
- Flowchart -> Flowchart of the solution to be developed.
- Program -> code commented on C language.

Reference: Version ,
Andersen Consulting Service Delivery

2. Exercise 1 – Conversor.

Software Requirements: Write a program for converting imaginary numbers.


:

2.1 Flowchart:

2.2 Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void Ret2Pol(float nR, float nI){

float zmod, ang, angGraus;

zmod = sqrt(pow(nR, 2) + pow(nI, 2));


ang = atan(nI/nR);
angGraus = ang * 180 / 3.14;

printf("The value of the modulo of Z is: %.2f\n", zmod);


printf("The value of your angle (θ) is: %.2f°", angGraus);

void Pol2Ret(float nZ, float nAng){

float nX, nY, ang;


Reference: Version ,
Andersen Consulting Service Delivery

ang = nAng * 3.14 / 180;


nX = nZ * cos(ang);
nY = nZ * sin(ang);
:

printf("The real part of your imaginary number (R) is: %.2f\n", nX);
printf("The imaginary part of your imaginary number (R) is: %.2f", nY);

int main(void) {

float number1=0, number2=0;


int choose=0;

printf("Choose Conversion:\n\n");
printf("[1] = Rectangular to polar.\n[2] = Polar to rectangle.");
fflush(stdout);
scanf("%d", &choose);

switch(choose){
case 1:
printf("Please enter the real part of your imaginary number (R): ");
fflush(stdout);
scanf("%f", &number1);

printf("Please insert the imaginary part of your imaginary number (X): ");
fflush(stdout);
scanf("%f", &number2);

Ret2Pol(number1, number2);
break;

case 2:
printf("Please enter the modulo of |Z|: ");
fflush(stdout);
scanf("%f", &number1);

printf("Please insert the value of your angle (θ): ");


fflush(stdout);
scanf("%f", &number2);

Pol2Ret(number1, number2);
break;

default:
printf("Invalid option :(");
}

return 0;
}

Reference: Version ,
Andersen Consulting Service Delivery

3. Exercise 2 – Hello inside the function.

Software Requirements: Write a function that simply prints a message “Hello!! I am from inside
of function” and call the function from main function.

3.1 Flowchart:

3.2 Code:

#include <stdio.h>
#include <stdlib.h>

void SayHello(void){
printf("Hello!! I am from inside of function.");
}

int main(void) {

SayHello();

Reference: Version ,
Andersen Consulting Service Delivery

return 0;
}

4. Exercise 3 – Sum of two integers.

Software Requirements: Write a function that takes two integers as input and prints their sum
inside the function.

Reference: Version ,
Andersen Consulting Service Delivery

4.1 Flowchart:

4.2 Code:

#include <stdio.h>
#include <stdlib.h>

void NumbersSum(int N1, int N2){

int result;

result = N1 + N2;

printf("The result is: %d", result);


}

Reference: Version ,
Andersen Consulting Service Delivery

int main(void) {

int num1 = 0, num2 = 0;

printf("Enter the first integer to make the sum: ");


:
fflush(stdout);
scanf("%d", &num1);

printf("Enter the second integer to make the sum: ");


fflush(stdout);
scanf("%d", &num2);

NumbersSum(num1, num2);

return 0;
}

5. Exercise 4 – EVEN or ODD.

Software Requirements: Write a function that takes an integer as input and prints EVEN if the
number is divisible by 2 otherwise prints ODD. Call this function from the main function.

5.1 Flowchart:

5.2 Code:

#include <stdio.h>
#include <stdlib.h>
Reference: Version ,
Andersen Consulting Service Delivery

void NumberAnalysis(int number){


if((number % 2) != 0){
printf("This number is ODD!");
}else{ :
printf("This number is EVEN!");
}
}

int main(void) {

int num;

printf("Insert a integer: ");


fflush(stdout);
scanf("%d", &num);

NumberAnalysis(num);

return 0;
}

6. Exercise 5 - All of divisors.

Software Requirements: Write a function that takes an integer as input and prints all of its
divisors. Call this function from the main function.
6.1 Flowchart:

Reference: Version ,
Andersen Consulting Service Delivery

6.2 Code:

#include <stdio.h>
#include <stdlib.h> :
#include <conio.h>

void NumberDivisors(int number){


int i, result;

for(i=0; i<=number; i++){


result = number % i;
if(result == 0){
printf("%d is a divisor of %d", i, number);
}
}
}

int main(void) {

int num=0;

printf("Insert a integer number: ");


fflush(stdout);
scanf("%d", &num);

NumberDivisors(num);

getche();

return 0;
}

7. Exercise 6 – Calculator

Software Requirements: Suppose you are going to make a simple calculator. Your task is to
prepare four functions- add(), subtract(), multiply() and divide() . Each function takes two
parameters and return the value after applying the corresponding action on those parameters.

Reference: Version ,
Andersen Consulting Service Delivery

7.1 Flowchart:

7.2 Code:

#include <stdio.h>
#include <stdlib.h>

void add(float n1, float n2){


float result;
result = n1 + n2;
printf("%.3f + %.3f = %.3f\n",n1, n2, result);
}
Reference: Version ,
Andersen Consulting Service Delivery

void subtract(float n1, float n2){


float result;
result = n1 - n2;
printf("%.3f - %.3f = %.3f\n",n1, n2, result);
} :
void multiply(float n1, float n2){
float result;
result = n1 * n2;
printf("%.3f * %.3f = %.3f\n",n1, n2, result);
}

void divide(float n1, float n2){


float result;
result = n1 / n2;
printf("%.3f / %.3f = %.3f\n",n1, n2, result);
}

int main(void) {
float num1, num2;
num1 = num2 = 0;

printf("Insert two number to calculate: ");


fflush(stdout);
scanf("%f", &num1);

printf("Insert two number to calculate: ");


fflush(stdout);
scanf("%f", &num2);

add(num1, num2);
subtract(num1, num2);
multiply(num1, num2);
divide(num1, num2);

return 0;
}

8. Exercise 7 – findMax.

Software Requirements: Write a function named findMax(). It takes three parameters and return
the maximum value among the three parameters. Call this function from the main function.

Reference: Version ,
Andersen Consulting Service Delivery

8.1 Flowchart:

8.2 Code:

#include <stdio.h>
#include <stdlib.h>

void findMax(float n1, float n2, float n3){


if(n1>n2>n3){
printf("The maximum value is: %.3f", n1);
}
if(n2>n1>n3){
printf("the maximum value is: %.3f", n2);
}
else{
Reference: Version ,
Andersen Consulting Service Delivery

printf("The maximum value is: %.3f", n3);


}
}

int main(void) { :
float num1, num2, num3;
num1 = num2 = num3 = 0;

printf("Insert 3 numbers:\n");
fflush(stdout);
scanf("%f %f %f", &num1, &num2, &num3);

findMax(num1, num2, num3);

return 0;
}

9. Exercise 8 – Dollar to BDT.

Software Requirements: Write a currency converter function named dollarToBDT(). It takes US


Dollar as input in a float variable and return the amount of R$. [Change rate: 1 USD = 3.86 R$].

9.1 Flowchart:

Reference: Version ,
Andersen Consulting Service Delivery

9.2 Code:

#include <stdio.h>
#include <stdlib.h>
:
void dollarToBDT(float n1){
float result;
result = n1 * 3.86;
printf("%.2f USD = %.2f R$", n1, result);
}

int main(void) {
float doll;

printf("How much in dollars? ");


fflush(stdout);
scanf("%f", &doll);

dollarToBDT(doll);

return 0;
}

10. Exercise 9 – Absolute value.

Software Requirements: Write a function that takes any real number and return its absolute value.
If user gives -5 as parameter, the function will return 5.

Reference: Version ,
Andersen Consulting Service Delivery

10.1 Flowchart:

10.2 Code:

#include <stdio.h>
#include <stdlib.h>

void AbsoluteValue(float n1){


if(n1 > 0){
printf("The absolute value is: %.2f", n1);
}
else{
printf("The absolute value is: %.2f", n1 * -1);
}
}

int main(void) {
float number=0;

printf("Insert a number: ");


fflush(stdout);
scanf("%f", &number);

AbsoluteValue(number);

return 0;
Reference: Version ,
Andersen Consulting Service Delivery

11. Exercise 10 – Area.

Software Requirements: Write a function which calculates the area of a: square and another
function that calculates the area of a circle. Ask the user if it is a square or a circle. Then, ask for
parameters and call the respective function to calculate the area.

11.1 Flowchart:

11.2 Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

Reference: Version ,
Andersen Consulting Service Delivery

void AreaSquare(float n1){


float result;
result = pow(n1, 2);

printf("The area of the side square %.2f is: %.2f",


: n1, result);
}

void AreaCircle(float r1){


float result;
result = 3.14 * pow(r1, 2);

printf("The area of the circle of radius %.2f is: %.2f", r1,


result);
}

int main(void) {
float num = 0;
char answer[7];

printf("Do you want to calculate the area of a SQUARE or the area


of a CIRCLE? ");
fflush(stdout);
gets(answer);

if((answer[0] == 83)||(answer[0]) == 115){


printf("What is the length of the side of the square? ");
fflush(stdout);
scanf("%f", &num);

AreaSquare(num);

return 0;

}
if((answer[0] == 67)||(answer[0] == 99)){
printf("What is the length of the radius of the circle? ");
fflush(stdout);
scanf("%f", &num);

AreaCircle(num);

return 0;
}
else{
printf("Not a valid option");
}

return 0;
}

12. Exercise 11 – Upper Case.

Software Requirements: Write a function named toUpperCase() which takes a small letter
alphabet as input and return the capital version. If user gives ‘a’ as parameter the function will
return ‘A’.

Reference: Version ,
Andersen Consulting Service Delivery

12.1 Flowchart:

12.2 Code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>

void toUpperCase(char c){


printf("%c ~> %c", c, toupper(c));
}

int main(void) {

char letter;

printf("Insert a small letter: ");


fflush(stdout);
scanf("%c", &letter);

toUpperCase(letter);

getche();
return 0;
}

Reference: Version ,

You might also like