{
/* Write a program in C++ to find the perfect numbers between 1 and
500.
The perfect numbers between 1 to 500 are:
6
28
496*/
int sum = 0;
int i = 1;
int j = 1;
for (i ; i <= 100; i++)
{
for (j; j <= 100; j++)
{
if (j <
i)//Line 55
{
if (i % j ==
0)
sum = sum + j;
}
}
if (sum == i)
cout << i
<< " ";
j = 1;//line 66
sum = 0;
}
return 0;
}
Can someone please explain the code above and how it works. what is the purpose of the comparison on line 55 and I on line 66?
In: Computer Science
1. Based upon the pseudocode given in “Background”, solve the three versions of add up to K problems: ◦ all pairs of numbers that add up to K ◦ all triples (set of three) that add up to K ◦ all possible subsets of numbers that add up to K: recursive version is given, you are asked to solve it iteratively. 2. Test the three functions using given test cases. 3. Measure the running time of algorithms under various input sizes.
#include <iostream>
#include <bitset>
#include <vector>
#include <time.h>
using namespace std;
const double BILLION = 1E9;
/* Print ALL pairs of number from the given array a[0...a_len-1] that adds up to K
@param intList: the list of int values
@param K: the sum we want to make
*/
void PairsAddupToK (int a[], int a_len, int K)
{
//TODO by you
}
/* Print ALL triples of numbers from the given array a[0...a_len-1] that adds up to K
@param intList: the list of int values
@param K: the sum we want to make
*/
void TriplesAddupToK (int a[], int a_len, int K)
{
//todo by you
}
/* Print ALL subsets of numbers from the given array, a[0...a_len-1 that adds up to K
this function solves the problem iteratively
NOTE: WE assume a_len<32 (so that we can use int (which uses 32 or 64 bits)
as code to decide subset
@param intList: the list of int values
@param K: the sum we want to make
*/
void SubsetsAddupToK (int a[], int a_len, int K)
{
//todo by you
}
//Recursive solution to SubsetAddupToK
/* This functions tries to use numbers from a[left...right] to make sum
@param a[] array of int
@n: a[left...right] is the list of numbers to use
@currentSum: current sum we have made using numbers in numsUsed
@finalSum: final sum that we need to make
@numsUsed: numbers that are used to get us here */
bool SubsetsAddupToK (int a[], int left, int right, int currentSum, int finalSum, vector<int> numsUsed)
{
//Three base cases:
//case 1: what remains to make is negative: we have overshot...
if (currentSum>finalSum)
return false;
//case 2: we made it!
// Result is only displayed at the base case.
if (currentSum==finalSum)
{
cout <<"Solution:";
for (int i=0;i<numsUsed.size();i++)
cout <<numsUsed[i]<<",";
cout <<endl;
return true;
}
//case 3: we don't have any number to use: fails.
if (left>right && currentSum < finalSum)
return false;
//General calse: what remains to make is positive, and we have numbers to use
bool res1=false, res2=false;
// Explore both possibilities: Use a[left] or not
// 1) include a[left] in the sum, i.e., make sum-a[left] using a[left+1...right]
vector<int> newNums = numsUsed;
newNums.push_back (a[left]); //now a[left] is used ...
res1 = SubsetsAddupToK(a, left+1, right, currentSum+a[left], finalSum,newNums);
// here the currentSum is increased by a[left], a[left] is added into list of
// numbers used...
// 2) do not include a[left] in the sum, i.e., make sum using a[left+1...right]
// currentSum, finalSum, numsUsed is not changed
res2 = SubsetsAddupToK (a, left+1, right, currentSum, finalSum, numsUsed);
//^+1 so that a[left] is not considered any more
//if either of the above two succeeded, return true
if (res1 || res2)
return true;
else
return false;
}
/* Compare the running time of three functions for input size n
@param n: the length of vecotr/list
*/
void MeasureRunningTime (int a[], int a_len)
{
struct timespec start, finish;
double r1, r2, r3, r4;
clock_gettime (CLOCK_REALTIME, &start);
PairsAddupToK (a, a_len, 100);
clock_gettime (CLOCK_REALTIME, &finish);
r1 = (finish.tv_sec - start.tv_sec)+
(finish.tv_nsec-start.tv_nsec)/BILLION;
clock_gettime (CLOCK_REALTIME, &start);
TriplesAddupToK (a, a_len, 100);
clock_gettime (CLOCK_REALTIME, &finish);
r2 = (finish.tv_sec - start.tv_sec)+
(finish.tv_nsec-start.tv_nsec)/BILLION;
clock_gettime (CLOCK_REALTIME, &start);
SubsetsAddupToK (a, a_len, 100);
clock_gettime (CLOCK_REALTIME, &finish);
r3 = (finish.tv_sec - start.tv_sec)+
(finish.tv_nsec-start.tv_nsec)/BILLION;
clock_gettime (CLOCK_REALTIME, &start);
vector<int> results;
SubsetsAddupToK (a, 0, a_len, 0, 100, results);
//^ current sum=0, final sum=100
clock_gettime (CLOCK_REALTIME, &finish);
r4 = (finish.tv_sec - start.tv_sec)+
(finish.tv_nsec-start.tv_nsec)/BILLION;
cout <<"n="<<a_len<<"\t";
cout <<"Pairs"<<"\t"<< r1<<"s\t";
cout <<"Triples"<<"\t"<< r2<<"s\t";
cout <<"Subsets"<<"\t"<< r3<<"s\t";
cout <<"SubsetsR"<<"\t"<< r4 <<"s\t"<<endl;
}
int main()
{
vector<int> numsUsed; //this is empty initially
int a5[5] = {50,90,20,30,10};
int a10[10] = {50, 33, 90, 2, 20,
72, 30,10, 92, 8};
int a20[20] = {50, 33, 11, 79, 90,
2, 20, 72, 30,10,
92, 8, 99, 101, 25,
71, 48, 72, 35, 9};
int a30[30] = {50, 33, 11, 79, 90,
2, 20, 72, 30,10,
92, 8, 99, 101, 25,
71, 48, 72, 35, 9,
37, 41, 55, 73, 67,
66, 22, 11, 6, 4};
//Testing
//Display all different ways to make 100 using a5[0...4]
int SumToMake = 100;
int curSum=0;
SubsetsAddupToK (a5, 0, 4, curSum, SumToMake, numsUsed);
/* Measuring and comparing running time
MeasureRunningTime (a5, 5);
MeasureRunningTime (a10, 10);
MeasureRunningTime (a20, 20); */
MeasureRunningTime (a30, 30);
}In: Computer Science
Write a recursive function, max_in_list(my_list), which takes an non-empty list, my_list, of integers as a parameter. This function calculates and returns the largest value in the list. The base case will probably deal with the scenario where the list has just one value. The recursive case will probably call the function recursively using the original list, but with one item removed.
Note: This function has to be recursive; you are not allowed to use loops to solve this problem!
| Test | Result |
|---|---|
lst = [1, 4, 5, 9] print(max_in_list(lst)) |
9 |
And
Write a recursive function, max_even_list(my_list), which takes a list, my_list, of integers as a parameter. This function calculates and returns the largest even number in the list. 0 is returned if there is no even number in the list.
Note: This function has to be recursive; you are not allowed to use loops to solve this problem!
| Test | Result |
|---|---|
lst = [1, 4, 5, 9] print(max_even_list(lst)) |
4 |
In: Computer Science
Compute the 10,000th element of the pseudorandom sequence generated by the Middle-Square algorithm with seed=1003.
In: Computer Science
public class Lab1 {
public static void main(String[] args) {
int array [] = {10, 20, 31, 40, 55, 60, 65525};
System.out.println(findPattern(array));
}
private static int findPattern(int[] arr) {
for (int i = 0; i < arr.length - 2; i++) {
int sum = 0;
for (int j = i; j < i + 2; j++) {
sum += Math.abs(arr[j] - arr[j + 1]);
}
if (sum == 20)
return i;
}
return -1;
}
}
QUESTION: Modify the given code such that the input array may wrap-around or over- flow.
Given the input array: data[] = {65515,65525,9,20,31,40,55} The function shall return: 0
In: Computer Science
Assignment for Health Information Technology
Search the web for an article related to a facility using patient generated health data(PGHD). Using a minimum of 125 words, tell me the type of healthcare setting, the type of of PGHD used, and if the facility found it beneficial/successful.
Provide the URL link to your article.
Use .org websites only (Ex. AHIMA.org, nln.nih.gov...etc)
In: Computer Science
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency.
(I am using c++ programming language)
In: Computer Science
3. Be creative, but realistic: Describe an example (of your own creation) of a system that would be susceptible to a race condition, and what might happen to cause the race condition in your imagined system. Then explain what might be the effect of that race condition in your particular system. The goal in this last part is to imagine a real-world system and to describe it in a way that the marker can understand as well as showing how it could be affected by a race condition.
In: Computer Science
True or False
1.An association is a description of a group of links with common behaviors and semantics.
2.An association is a logical construct, of which a reference is a generalization alternative.
3.Multiplicity specifies the number of instances of one object.
4.An association class is an association that is also a class.
5.An association class may have attributes, operation and participant in associations.
6.A qualified association is an association in which the objects in a “many” generalization are partially disambiguated by a qualifier.
7.Generalization is an important construct for both conceptual modeling and implementation.
8.To build complex systems, the developer must abstract different views of the system, build models using precise notations.
9.When analysts construct a model of the application, the detail implementation information will need to be considered.
10.The model has two dimensions – a view of a system and a stage of development.
In: Computer Science
You have a 1 ft radius dartboard placed in the middle of a 2 ft x 2 ft square backboard. You can estimate the value of Pi by randomly throwing darts at the board, because the ratio of the darts hitting the dartboard compared to those hitting the backboard (i.e. all of them), will be equal to the ratio of the area of the dartboard to the area of the board. Or:
Dh / Dt = Adb / Ab
where:
Dh = number of darts that hit the dartboard
Dt = total number of darts thrown
Adb = Area of the dartboard
Ab = Area of the board
We can calculate Adb and Ab from the
problem statement. You should be able to rearrange the resulting
equation to solve for Pi, given the sizes of the dartboard and
board, and the number of darts thrown and number that hit the
dartboard.
Write a VBA program that will randomly generate a location (x and
y) the dart will hit, determine if it hit the dartboard (hint: make
the center of the dartboard x=0, y=0), and then estimate the value
of Pi after each thrown dart). Stop once your estimated value is
within 0.01 of the actual value of Pi (you can use 3.1416 as the
actual value and the ABS() function may be useful here). Output the
estimated value and the total number of darts thrown on the 1st
worksheet (labeled "Darts")
Recommended: Output the number of darts thrown, number of darts
that have hit, and the estimated value of Pi on a new row on your
worksheet after each dart is thrown. Also, limit the total number
of darts thrown, and exit with a message if you reach the
limit.
In: Computer Science
Day of week
Write a program that asks the user for a date (year, then month, then day, each entered as a number on a separate line), then calculates the day of the week that date falls on (according to the Gregorian calendar) and displays the day name.
You may not use any date functions built into Python or its standard library, or any other functions that do this work for you.
Hint: Reverend Zeller may be useful, but his years start in a weird spot.
Give credit to your sources by including an exact, working URL in a comment at the top of your program.
LAB
In: Computer Science
C Programming question:
Develop a program named “sample” that prints the value of two
functions:
1. Function f1 takes a single input argument of type int and
returns the value of its input argument as float
2. Function f2 takes a single input argument of type char and
returns an int. The output of f2 is the result of the sum of the
values of the global variables, and the input
argument minus the value of char ‘a’ plus the value of char ‘A’
Structural Requirements:
1. Your executable must be named sample (your makefile must ensure
this)
2. Your program should not take any arguments
3. Your solution must have at least two C source files (e.g., a
main and file1.c), two header files (e.g., globals.h, and
externs.h), and a makefile
4. Your program should define two global variables (e.g., p and q),
both of type int, one with value 11 and the second with a value of
32
5. Your main should have two local variables: int i = 5; char c =
‘x’
6. Main should invoke f1 with the sum of i, p, and q as input, and
pass the value of c to f2
In: Computer Science
#include <stdio.h>
#include <stdlib.h> // required for atoi
int main(void) {
int i=0,n,num,filenum[100],pos;
int c;
char line[100]; //declaring string for storing data in
the line of text
FILE *fp; // declaring a FILE pointer
fp=fopen("numbers.txt","r"); // open a text file for
reading
while(fgets(line, sizeof line, fp)!=NULL)
{ // looping until end of the
file
filenum[i]=atoi(line);
//converting data in the line to integer and storing it into
array
i++;
}
n=i;
fclose(fp); //closing file
printf("Enter a number: ");
scanf("%d",&num);
while(num!=EOF) //looping until user enters end of the
file
{
pos=0;
for(i=0;i<n;i++)
{
if(num==filenum[i]) //if num is equal to
filenum[i]
{
pos=i+1;
}
}
if(pos>0)
printf("%d last
appears in the file at position %d\n",num,pos);
else
printf("%d does
not appear in the file\n",num);
printf("Enter a number: ");
scanf("%d",&num);
}
return 0;
}
=====================================================
(does anyone can help me to change this program in C++ and add some comments)
numbers.txt: 10 23 43 5 12 23 9 8 10 1 16 9
you must to have all of following output:
Enter a number: 10
10 last appears in the file at position 9
Enter a number: 29
29 does not appear in the file
Enter a number: 9
9 last appears in the file at position 12
Enter a number:
In: Computer Science
Search the web to discover the 10 most common user-selected passwords, and store them in an array. Design a JAVA program that prompts a user for a password, and continue to prompt the user until the user has not chosen one of the common passwords.
In: Computer Science
Research assignment for Health Information Technology.
Paper describing the content of SNOMED CT, it's purpose, how it was evolved, provide any history, an example of a code in SNOMED CT.
1-2 pages double spaced.
1 inch margins
Copy of citations articles or functional URLs. 2-3 citations from
.org websites. ex. ahima.org, nln.nih.gov...
In: Computer Science