Question

The task involves writing a C++ program that determines the prime numbers between 1 and 100. The steps you should follow to i

Requirements You must use user functions to perform the tasks. Use a two-dimensional array to solve this problem. Print the m

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Code:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int N = 10;//Dimension of table (n x n)

//Function to find primes in the table
void findPrimes(int table[N][N]){
    //Special case for 1
    table[0][0] = 0;
    bool primeFound = true;
    int prime = 2;// First prime number
    int nextPrime = 0;
    while(primeFound){
        primeFound = false;
        //Check table for all multiples of current prime
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++){
                //If number in table is multiple of current prime then set it as 0
                if(table[i][j] != prime && table[i][j] != 0 && table[i][j] % prime == 0)
                    table[i][j] = 0;
                
                //Find next prime number
                if(!nextPrime && table[i][j] > prime && table[i][j] != 0) {
                    nextPrime = table[i][j];
                    primeFound = true;//Prime number found in this check
                }
            }
        }
        prime = nextPrime;//Set next prime to be current prime
    }
}

//Function to print table on console
void printTable(int table[N][N]){
    for(int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << left << setw(5) << table[i][j];
        }
        cout << "\n";
    }
    cout << endl;
}

//Function to save table in a file
void saveTable(int table[N][N]){
    ofstream out("primes.txt");//Open file to write output
    for(int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            out << left << setw(5) << table[i][j] << " ";
        }
        out << "\n";
    }
    out.close();//Close the file
}

int main(int argc, char *argv[]) {
    int table[N][N];// 10x10 = 100

    //Fill the table
    for(int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            table[i][j] = i*N + j + 1;

    cout << "The initial table before the steps are taken:\n";
    printTable(table);//Print table

    findPrimes(table);//Find primes in the table

    cout << "The resulting table after the prime numbers are determined:\n";
    printTable(table);//Print table

    saveTable(table);//Save table in a file

    return 0;
}

Console output:

The initial table before the steps are taken: 1 2 3 4 5 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

File output:
C 2 3 10 5 C 7 C 0 10 11 10 13 10 10 C 17 C 19 10 23 25 0 C 10 0 C 29 10 0 35 31 0 C 10 C 37 C 10 41 10 43 10 0 10 47 10 49 1

Add a comment
Know the answer?
Add Answer to:
The task involves writing a C++ program that determines the prime numbers between 1 and 100....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Source: 2014 Nielsen 72 Prime Numbers A prime number is a number that is evenly divisible...

    Source: 2014 Nielsen 72 Prime Numbers A prime number is a number that is evenly divisible only by 1 and itself. The prime numbers less than 100 are listed below. 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Choose one of these numbers at random. Find the probability that a. The number is odd b. The sum of the digits is odd c....

  • Consider the below matrixA, which you can copy and paste directly into Matlab.

    Problem #1: Consider the below matrix A, which you can copy and paste directly into Matlab. The matrix contains 3 columns. The first column consists of Test #1 marks, the second column is Test # 2 marks, and the third column is final exam marks for a large linear algebra course. Each row represents a particular student.A = [36 45 75 81 59 73 77 73 73 65 72 78 65 55 83 73 57 78 84 31 60 83...

  • 1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand....

    1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand. b. Explain why you chose this technique over others. Year 3 Year 1 Year 2 Actual Actual Actual Forecast Forecast Forecast Demand Demand Demand Week 1 52 57 63 55 66 77 Week 2 49 58 68 69 75 65 Week 3 47 50 58 65 80 74 Week 4 60 53 58 55 78 67 57 Week 5 49 57 64 76 77...

  • CODE MUST BE WRITTEN IN SWIFT programming language Write a function that takes in two positive...

    CODE MUST BE WRITTEN IN SWIFT programming language Write a function that takes in two positive integers and prints every prime number between (and including) them Sample Input: prime(from: 0, to: 100) Sample Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

  • USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and...

    USE PYTHON PLEASE Write a function called is prime which takes a single integer argument and returns a single Boolean value representing whether the given argument is prime (True) or not (False). After writing the function, test it by using a loop to print out all the prime numbers from 1-100. To check your results, the prime numbers from 1-100 are: 2, 3, 5, 7, 11. 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,...

  • I need it in JAVA Write a program that randomly populates an array of size 100,...

    I need it in JAVA Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter....

  • 4. Write a logical function perfect Square that receives a positive integer number and checks if ...

    write the code in C please 4. Write a logical function perfect Square that receives a positive integer number and checks if it is a perfect square or not. Note: perfect square numbers are 4, 9,16,25,36 etc.... Write a main function that makes use of the perfect Square function to find and print all perfect squares between nl and n2. nl and n2 are end values of a range introduced by the user. ■ (inactive CAT EXE) Enter end values...

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • The given data is the grades for people in this class. The goal here is to...

    The given data is the grades for people in this class. The goal here is to determine the factors that effect student's Grade in the class. 4) Find the mean and median for the men's and the women's Quizzes. Gender Men Women 5) Test the claim that the majority of students at this class are women. F M F F M F F F F M M F F F M F F F F M M F F M...

  • 8. The following data are scores from a Physics final administered to 34 students. 81 76...

    8. The following data are scores from a Physics final administered to 34 students. 81 76 93 99 47 67 69 72 83 88 56 62 91 94 98 63 77 84 98 75 79 67 73 65 89 86 91 85 97 73 56 92 88 83 Use the Chart below to construct a Frequency Distribution with 5 classes (15 pts) Class Tally (This column is optional.) Frequency

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT