Concept explainers
The following code isn't working correctly and I am unable to figure out why. The for loop is never reached in the first if statement and the program never terminates. May someone please explain to me why that is.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <mpi.h>
int main(void)
{
int sum, comm_sz, my_rank, i, next, x;
int powTwo = 2;
int diff = 1;
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
srandom((unsigned)time(NULL) + my_rank);
x = random() % 10;
if (my_rank % powTwo == 0)
{
printf("IF----");
printf("Process %d generates: %d\n", my_rank, x);
for (i = 0; i < comm_sz; i++)
{
MPI_Recv(&x, 1, MPI_INT, i, my_rank , MPI_COMM_WORLD, MPI_STATUS_IGNORE);
sum += x;
printf("Current Sum=: %d\n", sum);
}
printf("The new divisor is:%d\n", powTwo);
powTwo *= 2;
diff *= 2;
} else if (my_rank % powTwo != 0) {
printf("ELSE----");
printf("Process %d generates: %d\n", my_rank, x);
MPI_Send(&x, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}else if (my_rank==0){
printf("Sum=: %d\n", sum);
}
MPI_Finalize();
return 0;
}
Step by stepSolved in 2 steps
- #include #include #include int main() { int i, j; printf("Columns | JIn"); for (i = 1; i< 4; ++i) { printf("Outer %6d\n", i); for (j = 0; j< i; ++j) { printf(" Inner%10d\n", j); } } /* heading of outer for loop */ /* heading of inner loop */ %3D return (0); } Create a new code modifying it to use "while loop" instead of the "for loop"arrow_forward// MichiganCities.cpp - This program prints a message for invalid cities in Michigan. // Input: Interactive // Output: Error message or nothing #include <iostream> #include <string> using namespace std; int main() { // Declare variables string inCity; // name of city to look up in array const int NUM_CITIES = 10; // Initialized array of cities string citiesInMichigan[] = {"Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"}; bool foundIt = false; // Flag variable int x; // Loop control variable // Get user input cout << "Enter name of city: "; cin >> inCity; // Write your loop here // Write your test statement here to see if there is // a match. Set the flag to true if city is found. // Test to see if city was not found to determine if // "Not a city in Michigan" message should be printed.…arrow_forward+ dgenuity.com/Player/ Semester A def fib(n): Complete the code for this recursive function. if n == 1: return 0 if n == 2: return 1 else: 3 return 5 # The first number in the list. # The second number in the list. # Add the previous two numbers. Mark this and return (n-1) + O (n-2) Σ B 0 Save and Exit Next Sign out English Martarrow_forward
- C++ This is what i have so far. Can you use a for loop for the code. #include <iostream> // for cin and cout#include <iomanip> // for setw() and setfill()using namespace std; // so that we don't need to preface every cin and cout with std:: int main(){int menuOption = 0; cout << "Choose from among the following options:\n"<< "1. Exit the program\n"<< "2. Display building\n"<< "Your choice -> ";cin >> menuOption;cout << endl; // Leave a blank line after getting the user input for the menu option. // See if exit was chosenif (menuOption == 1) {exit(0);} // Menu 2 if (menuOption == 2){cout << " /\\ " << endl;cout << " || " << endl;cout << " || " << endl;cout << " -- " << endl;cout << "|++|" << endl;cout << "====" << endl; } cout << endl;return 0;}arrow_forwardPls help ASAP. Language is C#arrow_forwardC++ Coding: Nested Loops Create a constant DIM. Set DIM to 7, and use a nested loop to display the following square matrix output. Output Example: - - - X - - -- - - X - - -- - - X - - -X X X O X X X- - - X - - -- - - X - - -- - - X - - -arrow_forward
- Write a nested if statement that will determine the student's final letter grade. Display the numeric and letter grades for the Final average and final exam. Write the nested code in python. import randomdef main(): # main function definition aGrades=[] # empty list to store Grades for i in range(7): # loop to read 7 inputs print("Input scores ",i+1," : ", end="") # a message to enter grade n=int(input()) # input grade aGrades.append(n) # add grade into list print("Grade list before randomize: ",aGrades) # print list random.shuffle(aGrades) # randomize list numExams=len(aGrades)-1 # compute numExams FinalExam=aGrades[-1] # store final grade TotalPoints=sum(aGrades)-aGrades[-1] # compute TotalPoints TestAverage=TotalPoints/numExams # compute TestAverage FinalAverage=TestAverage*.6 + FinalExam*.4 # compute FinalAverage print("Grade list after randomize is: ",aGrades) # print list print("Test Average =…arrow_forwardIn c++ Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, th backwards End each loop with a newline. Ex If courseGrades (7, 9, 11, 10), print: 7 9 11 10 10 11 9 7 Hint Use two for loops. Second loop starts with i=NUM VALS-1. (Notes) Note: These activities may test code with different test values. This activity will perform two tests, both with a 4-element array (int courseGrades(4) See 'How to Use zyBooks Also note: If the submitted code tries to access an invalid array element, such as courseGrades[9] for a 4-element array, the test may generate strange results. Or the test may crash and report "Program end never reached", in which case the system doesn't print the test case that caused the reported message 32344345730rity? 2 using namespace std; 3 4 int main() { 5 6 7 8 9 10 11 12 13 14 const int NUM VALS-4; int courseGrades [NUM VALS]; int i; for (i = 0; i > courseGrades[1]; } y Your solution goes here Yarrow_forwardThe term "for construct" refers to a loop in computer programming that processes a list of items. As a direct consequence of this, it continues to operate for as long as there are objects to be processed. Which of these two interpretations of the statement is correct?arrow_forward
- Python question Analysis: Invariants (Q16-17) For each of the following functions, identify the loop invariant, exit condition and post condition. Question 16 (Invariant 1) This function returns the list of integers that are multiples of both 3 and 7 that occurs within a given list. Identify the loop exit condition, the loop post-condition, and the loop invariant at the end of the loop, which shows the algorithm’s correctness. def multiples_count(lst): """ Input: a list of integers, lst Output: the list of integers in lst that are multiples of both 3 and 7 """ res = [] for i in range(len(lst)): if lst[i]%3 == 0 and lst[i]%7 == 0: res.append(lst[i]) # Identify the loop invariant here return res Question 17 (Invariant 2) This function checks if a given list (of comparable elements) is sorted in ascending order. Identify the loop exit condition, the loop post-condition, and the loop invariant at the end of each iteration of the loop, which…arrow_forwardc++ language using just one for looparrow_forwardComputer Science Part C: Interactive Driver Program Write an interactive driver program that creates a Course object (you can decide the name and roster/waitlist sizes). Then, use a loop to interactively allow the user to add students, drop students, or view the course. Display the result (success/failure) of each add/drop.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education