Design a C++ program that will run in batch mode (read its input from a file via
ID: 3679116 • Letter: D
Question
Design a C++ program that will run in batch mode (read its input from a file via Linux redirection). The input file will contain a series of integers that could be positive, negative, or zero.
For each integer read from the file display (in the form of a nicely formatted table) - see sample below 1) the number 2)if it is positive (greater than 0), the number of factors it has 3) if it is prime, the word "prime"4) if it is composite, the word "composite" 5) if it is neither prime nor composite, do not display a factor count or a descriptive term.
Continue processing integers until the end of file is encountered.
After all numbers have been processed, display the number of values in each category with appropriate labels.
Include labels for each column. Right justify numbers (assume input values will be >= -100,000,000 and <= 100,000,000).Left justify the words describing in each number (prime, composite).
Each value in the data file will be an integer between -100,000,000 and 100,000,000 inclusive and the numbers will be separated by whitespace (blanks or linefeeds).
The input file will not be empty.The last line of the input file will be terminated by a linefeed.
this is a sample of what should look like (except all lined up properly):
NUMBER FACTORS CATEGORY
17 2 prime
-2
4 3 composite
188 6 composite
0
31 2 prime
1054 8 composite
-56
123456 28 composite
Primes: 2
Composites: 4
Neither prime or composite: 3
need some help coming up with an algorithm to go with the if or while statements... thank you
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main(int argc,char *argv[])
{
int a[10000],eith,i,fact,prime=0,composit=0;
string cat;
FILE *file;
file=fopen(argv[0],"r");
i=0;
while(i!=EOF)
{
fscanf(file,"%d",&a[i]);
i++;
}
cout<<" number factor category ";
for(int j=0;j<i;i++)
{
fact=0;
if(a[j]<0)
cat=" ";
else
{ for(int k=2;k<a[j];k++)
{
if(a[i]%k==0) fact++;
}
if(fact>2)
{
cat="composit";
composit++;
}
else
{
cat="prime";
prime++;
}
}
cout<<" "<<a[j]<<" "<<fact<<" "<<cat;
}
eith=i-prime-composit;
cout<<"primes ="<<prime<<" composit ="<<composit<<" either nor="<<eith;
}