Occurences of numbers and alphabets using structure of arrays
Nov 14, 2014 at 2:57am UTC
to find the quantity of the form of each number / char appeared in this file. The form of a number means positive and negative valued number, or lower- and uppercase letters.
if the text file has 1 -1 2 -2 2 2 A c K it should display as
Number Positive Negative
1 1 1
2 3 1
Char lower Upper
A 0 1
c 1 0
K 0 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include<iostream>
#include<fstream> //Reading the file
#include<string> //using the string datatype
using namespace std;
struct num_det
{
int number; //Defining the structure as per therequirement
unsigned short positive;
unsigned short negative;
}stru_arr[100];
void insert_stru(num_det[],int ,unsigned short &); //Function signature
int main()
{
string filename;
int num[100],temp;
unsigned short ind=0,k=0,pos_count=0,neg_count=0;
ifstream inpt_file;
cout<<"Please enter the full path or file name present in the current working:" ;
cin>>filename;
inpt_file.open(filename);
while (inpt_file>>temp)
{
num[ind]=temp; //reading the file
ind++;
}
for (int i=0;i<ind;i++)
{
temp=num[i];
if (num[i]<0)
temp*=-1;
insert_stru(stru_arr,temp,k); //Moving the absolute values and distinct once into structure array.
}
for (int i=0;i<k;i++)
{
for (int j=0;j<ind;j++)
{
if (stru_arr[i].number==num[j])
++pos_count;
if ((stru_arr[i].number*-1)==num[j])
++neg_count;
}
stru_arr[i].positive=pos_count; //found positive instance
stru_arr[i].negative=neg_count; //found negative instance
pos_count=neg_count=0; //resetting back the counters
}
cout<<"Number\tPositive\tNegative" <<endl;
for (int i=0;i<k;i++)
cout<<stru_arr[i].number<<" \t" <<stru_arr[i].positive<<" \t\t" <<stru_arr[i].negative<<endl; //Displaying the output structural array
system("pause" );
return 0;
}
void insert_stru(num_det stru_arr[],int temp,unsigned short &k)
{
bool flag=true ;
for (int i=0;i<k;i++)
{
if (stru_arr[i].number==temp) //Find the distinct one or not.
flag=false ;
}
if (flag)
{
stru_arr[k].number=temp; //Distinct scenario found.
k++;
}
}
I just done with the number part but bit confusing about the alphabets?? any help
Last edited on Nov 14, 2014 at 2:57am UTC
Topic archived. No new replies allowed.