WEEK 6 FOR i_GotchA
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY GOOD WORK NICE FORMULA OR SOMETHING LIKE THAT, BUT ACTULLY HE CAN USE. THANK YOU.
Contains unread posts
Actions for Hartleys Function Code
Chad Hartley posted Nov 5, 2015 5:10 PM
This program will add an integer number and a decimal number up to 2 decimal places. I have included notes in the code to explain what each thing does. I hope I did this right. It compiles successfully.
PseudoCode
Start
Declare int O1; Stands for Output1
O1=sum; Sum is the functions name
Int sum()
Declare variables
Int num1;
Float num2;
Write “Enter a number.”
Scanf num1
Write”Enter a decimal number.”
Scanf num2
Return num1+num2
end
C Code
#include <stdio.h>
int sum();//prototype
int main()//calling program
{
//Declare a varaiable
int O1;
O1=sum();//main is calling sum one time.
//if I listed this twice it would run the function 'sum' twice.
// Example: if I add a new int (int O1, O2) and declare O2 to
//be O2=sum then the function would run twice.
}
int sum ()//function 'sum'
{
int num1;// Declare intergers/variables
float num2;
printf("Enter a number.\n");
scanf("%d",&num1);// Take first input and assign it to num1
printf("Enter a decimal number.\n");
scanf("%.2f",&num2);
//Can use the printf statement but when you are calling an integer you can use the return.
//printf("The sum of %d, %d, is %d", num1,num2,num1+num2);
return num1+num2;
}
ADD COMMENT HERE
Contains unread posts
Joshua Ray posted Nov 5, 2015 2:33 PM
float tmp int i function float chaos(float num) { for i < 20 num = 3.9*num*(1-num) print num } main print "Program description" print "Request input btw 0 and 1" tmp = input chaos(tmp) /* * File: main.c * Author: JaiEllRei * * Created on November 5, 2015, 2:04 PM */ #include <stdio.h> #include <stdlib.h> float chaos(float num); int main(void) { float tmp; printf("This program illustrates a choatic function. \n"); printf("Input a number between 0 and 1: "); scanf("%f", &tmp); chaos(tmp); } float chaos(float num) { for (int i=0; i<20; i++){ /*Chaotic Formula*/ num = 3.9 * num * (1-num); printf("%.3f \n", num); } }
This program illustrates a choatic function. Input a number between 0 and 1: .2 0.624 0.915 0.303 0.824 0.566 0.958 0.156 0.514 0.974 0.098 0.345 0.881 0.409 0.943 0.210 0.647 0.891 0.379 0.918 0.293
ADD COMMENT HERE
//MPH to KPH Conversion Function
Function KPHConv(value) as float
Set KPHConv = value*1.609344
End Function
Pseudocode for simple conversion program calling function
//Declare function
// MPH to KPH Conversion Function
Function KPHConv(value) as float
Set KPHConv = inMPH*1.609344
End Function
//Start Main
Main
//Declare variables
Declare inMPH, outKPH as Float
//Initialize variables
inMPH = 1.0f;
outKPH = 0.0f;
Print “Enter a positive value for Miles Per Hour (MPH) or ‘0’ to exit the program
Input inMPH
//Loop while positive number
While inMPH > 0
//Call the MPH to KPH conversion function
Set outKPH = KPHConv(inMPH)
Print “inMPH miles per hour converts to outKPH kilometers per hour”
Input next inMPH value
END While
// End of Main program
End program
C Code
#include <stdio.h>
//Week 6 Discussion
//Author: Ken Hartman
//CMIS 102 7380
//Prof. Stephen Grady
//This program will convert a value for Miles Per Hour (MPH)
//to Kilometers Per Hour (KPH)
/* MPH to KPH Conversion Function */
float KPHConv(float value)
{
return value*1.609344;
}
/* Start of Main program */
int main()
{
/* Declare variables to be used as float */
float inMPH;
float outKPH;
/* Initialize the variables needed */
inMPH = 0.0f;
printf("Enter a value for Miles Per Hour (MPH) or 0 to exit the program \n");
scanf("%f", &inMPH);
/* Loop while a positive number is entered */
while (inMPH > 0.0)
{
outKPH = KPHConv(inMPH);
printf("\n %.2f miles per hour converts to %.4f kilometers per hour\n",inMPH, outKPH);
scanf("%f", &inMPH);
}
return 0;
}
ADD COMMENT HERE!!!!
Joseph Sturdivant posted Nov 6, 2015 11:52 PM
#include <stdio.h>
/* First question output:
* High number = 4
* Low number = 7 [because gettable function receive and return integer value]
*/
// function which looking for largest number in array
int largest(int array[], int size);
int function(int x) { return x; }
// main function
int main() {
int array[100]; // array where integers will be stored
int count=0; // how many integers entered
int number; // represent another integer from input
printf("High = %d\n",function(7.8));
printf("Enter integers (0-terminate): \n");
do {
scanf("%d",&number); // read one integer
array[count++]=number; // store it into array
} while(number!=0);
// print result of "largest" function
printf("Largest number is %d\n",largest(array,count-1));
return 0;
}
int largest(int array[], int size) {
int i;
int max=array[0]; // largest value will be stored here
for(i=1; i<size; i++) {
if(array[i]>max) max=array[i];
}
return max; // return largest value
}
ADD COMMENT HERE
Thomas Week 6- Solving Functions Discussion
Contains unread posts
Actions for Thomas Week 6- Solving Functions Discussion
Thomas Blass posted Nov 5, 2015 8:08 PM
1. Two arrays are provided, one float array and one integer array. Functions are used to display the highest number from the float array and the lowest number from the integer array. The first function (gettable) will determine the lowest number from the designated set in the “main” function. Then the second function (getTable) will determine the highest number from the designated set in the “main” function. So the expected output for this pseudocode should be:
“High number= 4
Low number= 7.8”
The only possible error I see here is that there might be an error with data types. It looks like when the “table1” array is defined it is of the float data type, but the function pseudocode for “getTable” is requesting integer data type. I thought that this might cause an error when the function “getTable” is called because of different data types.
Has anyone noticed the same or am I incorrect? Professor? Class?
2. Algorithm and C code posted below! Took me a few tries to figure out the best approach here.
//Pseudocode
//This program will accept input of positive integers terminated by 0, then determine
//the largest integer that was entered
//Largest integer function
Declare function largest_number as integer
Declare largest,j as integer
Set j=0
Set largest=user_array element 0
While(j<100000)
If(largest<user_array[j]) then
Set largest=user_array[j]
End if
Increment j
End while
Return largest
End function
Main function
//Declare variables
Declare user_array[100000] as integer
Declare number, i, q, largest as integer
//Initialize the array
for(q=0;q<100000;q++)
user_array[q]=0
//Initialize variables
set i=0
//While loop for array entry
while(i<100000)
print “please enter a positive integer: “
input number
if(number==0) then
break
end if
set user_array[i]=number
increment i
//Print largest number
largest=largest_number(user_array)
print “The largest number is: “
return 0
End program
C code:
//
// main.c
// CMIS102 Solving Functions Discussion
// Created by Thomas Blass on 11/5/15.
#include <stdio.h>
#include <stdlib.h>
//largest integer function
int largest_number(int user_array[])
{
int largest,j;
j=0;
largest=user_array[0];
while(j<100000)
{
if(largest<user_array[j])
{
largest=user_array[j];
}
j++;
}
return largest;
}
int main()
{
//Declare variables
int user_array[100000];
int number,i,q,largest;
//Initialize array
for(q=0;q<100000;q++)
{
user_array[q]=0;
}
//Initialize variables
i=0;
//while loop for array entry
while(i<100000)
{
printf("Please enter a positive number: \n");
scanf("%d",&number);
if(number==0)
break;
user_array[i]=number;
i++;
}
//Print largest number
largest=largest_number(user_array);
printf("The largest number is: %d\n",largest);
return 0;
}
ADD COMMENT HERE
Vathavadee Smakpunt posted Nov 4, 2015 10:12 PM
Hello all;
1) The output I got is:
High number = 4
Low number =7.8
2) I created function to return maximum number from 3 values.
#include <stdio.h>
//Set getMax function from array
int getMax(int number[]) {
//Declare integer i, max int i, max;
//Set value for max max = number [0];
//Set i to zero i=0;
//Set condition while loop while (i< 3) { scanf("%d", &number[i]); if(max<number[i]) { max=number[i]; } i=i+1; } return max; } main () {
/*Declare number[3], max, and i as integer*/ int number[3], max, i;
/*Set value for max*/ max = getMax(number[0]);
/*Print result*/ printf("Max is =%d\n",max); }