Results 1 to 1 of 1

Thread: Division with Arrays in C++

  1. #1
    Join Date
    Apr 2016
    Posts
    1
    Qt products
    Qt3
    Platforms
    Windows Android

    Default Division with Arrays in C++

    //Hello QtC,
    //I'm having more trouble with this problem than I expected, if anyone can suggest some fixes it would be greatly appreciated!

    //This program should read numbers from one file, determine if they are prime or not, then output the numbers into a SECOND file
    //that show the input Numbers, the Quotients of inputs divided by 5(with precision 2), Prime numbers, and the Not Prime numbers in an array:
    //I use Visual Studio 2013

    #include <iostream>
    #include <fstream>
    using namespace std;

    //function prototypes:
    bool determinePrime(int num); //determines if the number is prime (true) or not (false)

    void printOutput(int a[], int size); //shows the result to user

    void div5Quotient(); //calculates the quotient by dividing input by 5

    //opens new file (prog_outputs) and writes in results
    // ofile.open("prog_outputs.txt ;
    // //array width
    // for (!filenameIN.eof()){
    // cin >> arr[w]
    // w++
    // out<< SETw[15]<<ARR[x]<<"."
    //out.sef(IOS:showpoint);
    //OUT.Precision(2);
    //}

    int main()
    {
    int count = 0;
    int a[11];//size of the array, more than the 10 entries in data file
    int b = 0;
    int divisor;
    int c; //array for values/5
    int x=5;


    ifstream infile;

    // 1. Input
    infile.open("prog_inputs.txt");//open the text file

    if (!infile.is_open())
    {
    cout << "Unable to open file! ";
    exit(1); // if file didn't open
    }

    while (!infile.eof()) //reads from the file until the end
    {
    //To make array for column---reading from the file into x
    //the updated array will have elements for every input
    infile >> a[count];

    count++;
    }
    // cout <<a[i]<<endl;

    // Process our data
    // print our data
    printOutput(a, count);
    }

    //2. To determine if number is prime:
    bool determinePrime(int num){

    // check for NOT prime
    for (int i = 2; i < num; i++)
    {
    if (num % i == 0)
    {
    return false;
    }
    }

    // if check completes, means it is a prime
    return true;
    }


    //shows the results as an array to user
    void printOutput(int a[], int size) {

    for (int i = 0; i < size; i++)
    {
    if (determinePrime(a[i]))
    {
    cout << a[i] << "\t" << "Prime" << endl;
    }
    else
    {
    cout << a[i] << "\t" << "Not Prime" << endl;
    }
    }

    double div5Quotient() {

    a/x;
    //OUT.Precision(2);
    }

    }

    //Prime number check function works fine but I can't simply divide the values by 5, or output to a new file
    Last edited by GorditaCRUNCH; 20th April 2016 at 04:02.

Similar Threads

  1. Arrays in QScript
    By tsrplatelayer in forum Newbie
    Replies: 3
    Last Post: 6th March 2015, 12:12
  2. cell division problem on QTextDocument
    By sipahi in forum Newbie
    Replies: 1
    Last Post: 7th February 2013, 16:33
  3. Replies: 5
    Last Post: 5th November 2010, 17:26
  4. Scale division problem
    By Indalo in forum Qwt
    Replies: 0
    Last Post: 2nd December 2009, 08:36
  5. creating 2-d arrays
    By therealjag in forum Newbie
    Replies: 1
    Last Post: 21st February 2006, 17:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.