SlideShare a Scribd company logo
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <conio.h>
using namespace std;
//Function reads characters from the user and controls that user
enters integer number
bool read_integer(istream& fin,int &number);
//Function loads array from txt file of special format
void load_from_file(int a[],const char filename[]);
//Function save array to txt file of special format
void save_to_file(int a[],const char filename[]);
//Prints array and the active element
void print_array(int a[]);
//Function prints menu of the program
void print_menu();
//Function will insert an integer before the selected integer and
makes the newly inserted integer active.
void insert(int a[],int num_array);
//Function deletes the active integer.
void delete_active(int a[]);
//Function sorts the list in ascending order
void sort(int a[]);
//Function selects the next integer in the list.
void select(int a[]);
//Function moves the selected integer one position to the right
void move_right(int a[]);
//Function moves the selected integer one position to the left
void move_left(int a[]);
int main()
{
const int MAX_LENGTH = 10; // defines the maximum number
of elements in the third string
int values[MAX_LENGTH+2];
char scanCode;
// load information from the file
load_from_file(values,"numbers.txt");
// cycle for user dialog
do
{
// print menu and array
print_menu();
print_array(values);
scanCode = getch(); // read pressed key
if (scanCode == ' ')
scanCode = getch(); // clean ' '
if ((scanCode == 0)||(scanCode == 224)) // for extended control
keys
scanCode = getch();
switch (scanCode)
{
case '1': // insert integer before active ('1' or 'insert')
case 82:
insert(values,MAX_LENGTH);
break;
case '2': // delete active ('2' or 'delete')
case 83:
delete_active(values);
break;
case '3': // sort values (3 or F2)
case 60 :
sort(values);
break;
case '4': // select element ('4' or 'down arrow')
case 80:
select(values);
break;
case '5': // move right ('5' or 'right arrow')
case 77:
move_right(values);
break;
case '6': // move left ('6' or 'left arrow')
case 75:
move_left(values);
break;
case '7': // exit the program ('7' or 'F1')
case 59:
// save array to file
save_to_file(values,"numbers.txt");
//exit
cout<<"End of program"<<endl;
return 0;
break;
}
} while ((scanCode!='7')&&(scanCode!=59)); // 59 is 'F1'
code
return 0;
}
void new_line(istream& fin)
{
char symbol;
do
{
fin.get(symbol);
} while (symbol != ' ');
}
bool read_integer(istream& fin,int &number)
{
char symbol;
number = 0; // initialize the number
bool good_int;
good_int = true;
fin.get(symbol);
if (symbol == ' ')
fin.get(symbol); // clean if we have ' '
while (symbol !=' ')
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
}
return good_int;
}
void load_from_file(int a[],const char filename[])
{
ifstream fin;
fin.open(filename);
if (fin.fail( ))
{
cout << "Input file opening failed. ";
exit(1);
}
fin >> a[0]; // read number of elements in the list
fin >> a[1]; // read index of active element
// read elements of the list
for (int i=0; i<a[0]; i++) // first 2 elements are length and active
element
{ // so elements of 3rd line in
array[2],array[3]...array[MAX_LENGTH+1]
fin >> a[i+2];
}
fin.close(); // close stream
return;
}
void save_to_file(int a[],const char filename[])
{
ofstream fout;
fout.open(filename);
if (fout.fail())
{
cout << "Output file opening failed. ";
exit(1);
}
fout << a[0] << endl; // write length of list to file
fout<< a[1] << endl; // write index if active element to file
// write elements of list to file, separated by space character
for (int i = 0; i < a[0]; i++)
fout<<a[i+2]<<' ';
fout<<endl;
fout.close(); //close out file
return;
}
void print_array(int a[])
{
cout<<endl;
// check length of the array
if (a[0] == 0)
cout<<"empty";
for (int i = 1; i <= a[0]; i++)
if (a[1]== i) // number of active element
cout<<"["<<a[i+1]<<"] ";
else
cout<< a[i+1]<< " ";
// array[1] indicates the number of active element in line,
starting from 1
cout<<endl;
}
void print_menu()
{
// clear screen system("cls");
system("cls"); // for(int i=0;i<25;i++) cout<<endl;
cout.setf(ios::left);
cout <<setw(15)<<"1.Insert"<<""Insert" key"<<endl;
cout <<setw(15)<<"2.Delete"<<""Delete" key "<<endl;
cout <<setw(15)<<"3.Sort"<<""F2" key "<<endl;
cout <<setw(15)<<"4.Select"<<""Down Arrow" key
"<<endl;
cout <<setw(15)<<"5.Move Right"<<""Right Arrow" key
"<<endl;
cout <<setw(15)<<"6.Move Left"<<""Left Arrow" key
"<<endl;
cout <<setw(15)<<"7.Exit"<<""F1" key"<<endl<<endl;
}
void insert(int a[],int num_array)
{
int value;
bool result;
// checking, that list is full
if (a[0]==num_array)
{
cout<<"List of integers is full. Insertion is not
possible."<<endl;
return;
}
do
{
cout<<"Enter inserted integer : ";
result = read_integer(cin,value); // read integer with control
if (!result)
cout<<"You entered a wrong integer value"<<endl;
} while (!result);
// shift all elements after active one position right
for (int i = a[0]; i >= a[1]; i--)
a[i+2] = a[i+1]; // +2 correction (first two elements are amount
and active element)
// check if array was empty (active was not selected)
if (a[1] == 0)
a[1] = 1;
// insert new element at active position
a[a[1]+1] = value;
// increase the total amount of elements in array
a[0] ++;
return;
}
void delete_active(int a[])
{
if (a[0]==0)
{
cout<<"List of integers is empty. Deletion is not
possible."<<endl;
return;
}
// shift active element and all elements after active one position
left
for (int i= a[1]; i < a[0]; i++)
a[i+1] = a[i+2];
// decrease the total amount of elements in array
a[0] --;
// correct the index of active element
if (a[1]>a[0])
a[1] = a[0];
return;
}
void sort(int a[])
{
if (a[0]<=1) // sorting illegal (one, or none elements in the list)
return;
int active = a[ a[1] + 1 ]; // save active integer element
// bubble sort of elements array[2]...array[ array[0] ]
for (int i = a[0] + 1 ; i > 2; i--)
for (int j = 2; j < i; j++)
if (a[j] > a[j+1])
{
int temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
// search new index of saved active integer
for (int i = 2; i <= a[0] + 1; i++)
if (a[i] == active)
{
a[1] = i - 1; // write index of active integer
break;
}
return;
}
void select(int a[])
{
if (a[0]<=1) // selection illegal (one, or none elements in the
list)
return;
// if the last integer is selected
if (a[1] == a[0])
a[1] = 1; // select the first item in the array
else
a[1]++; // select next element in the list
return;
}
void move_right(int a[])
{
if (a[1]==a[0]) // move right is illegal (if the last integer is
selected)
return;
int index = a[1]; // index of active element
// swap active element (index in array[1]) with the next element
int tmp = a[index+1];
a[index+1] = a[index+2];
a[index+2] = tmp;
// correct index of active element
a[1]++;
return;
}
void move_left(int a[])
{
// if the first integer is selected (array[1] = 1) or there is not
selected element (array[1] = 0)
if (a[1]<=1) // move left is illegal
return;
int index = a[1]; // index of active element
// swap active element (index in array[1]) with the previous)
int tmp = a[index];
a[index] = a[index+1];
a[index+1] = tmp;
// correct index of active element
a[1]--;
return;
}
bool read_integer(istream& fin,int &number)
{
char symbol;
number = 0; // initialize the number
bool good_int;
good_int = true;
fin.get(symbol);
if (symbol == ' ')
fin.get(symbol); // clean if we have ' '
while (symbol !=' ')
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
}
return good_int;
}
I can input all positive intergal in this program, but I cannot
input negative intergal. this program require that i just allow to
input integer, and i need to input both negative and positive
integer. what should I do? CAUsers Roder Desktopl Project
2.exe Insert" key "Delete" key F2" key Down Arrow" key 1.
Insert 2. Delete 3. Sort 4. Select 5. Move Right "Right Arrow"
key 6. Move Left 7. Exit "Left Arrow" key "F1" key -8-3 0 1
5 [12] 22 55 66 Enter inserted integer 8 You entered a wrong
integer value Enter inserted integer:
Solution
The below function has an actual problem in reading negative
numbers. The function is designed to check the weather the
entered number is positive or not a number.
It reads each and every character and checks weather it is digit
or not if it's not a digit then function return false to the insert
function.
If you enter 345 as input then reads 3,4 and 5 individually and
checks digit or not. In your case if you enter -8 function reads
'-' and 8 individually. '-' is not a digit hence your function
return false.
Here just modify the function as given below
bool read_integer(istream& fin,int &number)
{
char symbol;
number = 0; // initialize the number
bool good_int;
good_int = true;
fin.get(symbol);
if (symbol == ' ')
fin.get(symbol); // clean if we have ' '
int count=0; // count variable to check first char '-' or not
int negNumber=0;
while (symbol !=' ')
{
if(count==0) // checking first character weather enterd '-' or
not
{
if(symbol=='-') // if it's negative number
{
fin.get(symbol); // next char
negNumber=1; // detecting negative number
count++; //incrementing count
}
else //if it's positive normal operation
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
count++;
}
}
else // for the next chars as usual operation
{
if (isdigit(symbol)) // correction of the number
number = number*10 + (symbol-'0');
else // user enters forbidden symbol
good_int = false;
fin.get(symbol);
count++;
}
}
if(negNumber==1)
number=number-(2*number); //loading negative number using
basic formula ; i.e 10-2*10=-10;
return good_int;
}

More Related Content

Similar to #include fstream#include iostream#include cstdlib#includ.docx

Please answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docxPlease answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docx
cgraciela1
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdf
arorasales234
 
c programming
c programmingc programming
c programming
Arun Umrao
 
-- Reminder that your file name is incredibly important- Please do not.docx
-- Reminder that your file name is incredibly important- Please do not.docx-- Reminder that your file name is incredibly important- Please do not.docx
-- Reminder that your file name is incredibly important- Please do not.docx
Adamq0DJonese
 
Arrays
ArraysArrays
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
GkhanGirgin3
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
aptcomputerzone
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
aquazac
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
JkPoppy
 
DAA Lab Work.docx
DAA Lab Work.docxDAA Lab Work.docx
DAA Lab Work.docx
Deepusri2000Srivasta
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
Arrays
ArraysArrays
Arrays
fahadshakeel
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx
ajoy21
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
arri2009av
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
Pranav Ghildiyal
 

Similar to #include fstream#include iostream#include cstdlib#includ.docx (20)

Please answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docxPlease answer the 4 questions using C- The expected output is shown be.docx
Please answer the 4 questions using C- The expected output is shown be.docx
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdf
 
c programming
c programmingc programming
c programming
 
week-15x
week-15xweek-15x
week-15x
 
-- Reminder that your file name is incredibly important- Please do not.docx
-- Reminder that your file name is incredibly important- Please do not.docx-- Reminder that your file name is incredibly important- Please do not.docx
-- Reminder that your file name is incredibly important- Please do not.docx
 
Arrays
ArraysArrays
Arrays
 
week-16x
week-16xweek-16x
week-16x
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
DAA Lab Work.docx
DAA Lab Work.docxDAA Lab Work.docx
DAA Lab Work.docx
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Arrays
ArraysArrays
Arrays
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
 

More from ajoy21

Please complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxPlease complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docx
ajoy21
 
Please cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxPlease cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docx
ajoy21
 
Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docx
ajoy21
 
Please check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxPlease check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docx
ajoy21
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
ajoy21
 
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxPlease attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
ajoy21
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
ajoy21
 
Please answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxPlease answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docx
ajoy21
 
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxPlease answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docx
ajoy21
 
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxPlease answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docx
ajoy21
 
Please answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxPlease answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docx
ajoy21
 
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxPlease answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docx
ajoy21
 
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxPlease answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docx
ajoy21
 
Please answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxPlease answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docx
ajoy21
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxPlease answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docx
ajoy21
 
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxPlease answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docx
ajoy21
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxPlease answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docx
ajoy21
 
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxPlease answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docx
ajoy21
 
Please answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxPlease answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docx
ajoy21
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
ajoy21
 

More from ajoy21 (20)

Please complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxPlease complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docx
 
Please cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxPlease cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docx
 
Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docx
 
Please check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxPlease check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docx
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
 
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxPlease attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
 
Please answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxPlease answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docx
 
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxPlease answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docx
 
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxPlease answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docx
 
Please answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxPlease answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docx
 
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxPlease answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docx
 
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxPlease answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docx
 
Please answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxPlease answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docx
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxPlease answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docx
 
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxPlease answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docx
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxPlease answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docx
 
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxPlease answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docx
 
Please answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxPlease answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docx
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
 

Recently uploaded

Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 

Recently uploaded (20)

Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 

#include fstream#include iostream#include cstdlib#includ.docx

  • 1. #include <fstream> #include <iostream> #include <cstdlib> #include <iomanip> #include <conio.h> using namespace std; //Function reads characters from the user and controls that user enters integer number bool read_integer(istream& fin,int &number); //Function loads array from txt file of special format void load_from_file(int a[],const char filename[]); //Function save array to txt file of special format void save_to_file(int a[],const char filename[]); //Prints array and the active element void print_array(int a[]); //Function prints menu of the program void print_menu(); //Function will insert an integer before the selected integer and makes the newly inserted integer active. void insert(int a[],int num_array); //Function deletes the active integer. void delete_active(int a[]); //Function sorts the list in ascending order void sort(int a[]); //Function selects the next integer in the list. void select(int a[]); //Function moves the selected integer one position to the right void move_right(int a[]); //Function moves the selected integer one position to the left void move_left(int a[]); int main() { const int MAX_LENGTH = 10; // defines the maximum number of elements in the third string int values[MAX_LENGTH+2];
  • 2. char scanCode; // load information from the file load_from_file(values,"numbers.txt"); // cycle for user dialog do { // print menu and array print_menu(); print_array(values); scanCode = getch(); // read pressed key if (scanCode == ' ') scanCode = getch(); // clean ' ' if ((scanCode == 0)||(scanCode == 224)) // for extended control keys scanCode = getch(); switch (scanCode) { case '1': // insert integer before active ('1' or 'insert') case 82: insert(values,MAX_LENGTH); break; case '2': // delete active ('2' or 'delete') case 83: delete_active(values); break; case '3': // sort values (3 or F2) case 60 : sort(values); break; case '4': // select element ('4' or 'down arrow') case 80: select(values); break; case '5': // move right ('5' or 'right arrow') case 77: move_right(values);
  • 3. break; case '6': // move left ('6' or 'left arrow') case 75: move_left(values); break; case '7': // exit the program ('7' or 'F1') case 59: // save array to file save_to_file(values,"numbers.txt"); //exit cout<<"End of program"<<endl; return 0; break; } } while ((scanCode!='7')&&(scanCode!=59)); // 59 is 'F1' code return 0; } void new_line(istream& fin) { char symbol; do { fin.get(symbol); } while (symbol != ' '); } bool read_integer(istream& fin,int &number) { char symbol; number = 0; // initialize the number bool good_int; good_int = true; fin.get(symbol); if (symbol == ' ') fin.get(symbol); // clean if we have ' ' while (symbol !=' ')
  • 4. { if (isdigit(symbol)) // correction of the number number = number*10 + (symbol-'0'); else // user enters forbidden symbol good_int = false; fin.get(symbol); } return good_int; } void load_from_file(int a[],const char filename[]) { ifstream fin; fin.open(filename); if (fin.fail( )) { cout << "Input file opening failed. "; exit(1); } fin >> a[0]; // read number of elements in the list fin >> a[1]; // read index of active element // read elements of the list for (int i=0; i<a[0]; i++) // first 2 elements are length and active element { // so elements of 3rd line in array[2],array[3]...array[MAX_LENGTH+1] fin >> a[i+2]; } fin.close(); // close stream return; } void save_to_file(int a[],const char filename[]) { ofstream fout; fout.open(filename); if (fout.fail()) {
  • 5. cout << "Output file opening failed. "; exit(1); } fout << a[0] << endl; // write length of list to file fout<< a[1] << endl; // write index if active element to file // write elements of list to file, separated by space character for (int i = 0; i < a[0]; i++) fout<<a[i+2]<<' '; fout<<endl; fout.close(); //close out file return; } void print_array(int a[]) { cout<<endl; // check length of the array if (a[0] == 0) cout<<"empty"; for (int i = 1; i <= a[0]; i++) if (a[1]== i) // number of active element cout<<"["<<a[i+1]<<"] "; else cout<< a[i+1]<< " "; // array[1] indicates the number of active element in line, starting from 1 cout<<endl; } void print_menu() { // clear screen system("cls"); system("cls"); // for(int i=0;i<25;i++) cout<<endl; cout.setf(ios::left); cout <<setw(15)<<"1.Insert"<<""Insert" key"<<endl; cout <<setw(15)<<"2.Delete"<<""Delete" key "<<endl; cout <<setw(15)<<"3.Sort"<<""F2" key "<<endl; cout <<setw(15)<<"4.Select"<<""Down Arrow" key
  • 6. "<<endl; cout <<setw(15)<<"5.Move Right"<<""Right Arrow" key "<<endl; cout <<setw(15)<<"6.Move Left"<<""Left Arrow" key "<<endl; cout <<setw(15)<<"7.Exit"<<""F1" key"<<endl<<endl; } void insert(int a[],int num_array) { int value; bool result; // checking, that list is full if (a[0]==num_array) { cout<<"List of integers is full. Insertion is not possible."<<endl; return; } do { cout<<"Enter inserted integer : "; result = read_integer(cin,value); // read integer with control if (!result) cout<<"You entered a wrong integer value"<<endl; } while (!result); // shift all elements after active one position right for (int i = a[0]; i >= a[1]; i--) a[i+2] = a[i+1]; // +2 correction (first two elements are amount and active element) // check if array was empty (active was not selected) if (a[1] == 0) a[1] = 1; // insert new element at active position a[a[1]+1] = value; // increase the total amount of elements in array a[0] ++;
  • 7. return; } void delete_active(int a[]) { if (a[0]==0) { cout<<"List of integers is empty. Deletion is not possible."<<endl; return; } // shift active element and all elements after active one position left for (int i= a[1]; i < a[0]; i++) a[i+1] = a[i+2]; // decrease the total amount of elements in array a[0] --; // correct the index of active element if (a[1]>a[0]) a[1] = a[0]; return; } void sort(int a[]) { if (a[0]<=1) // sorting illegal (one, or none elements in the list) return; int active = a[ a[1] + 1 ]; // save active integer element // bubble sort of elements array[2]...array[ array[0] ] for (int i = a[0] + 1 ; i > 2; i--) for (int j = 2; j < i; j++) if (a[j] > a[j+1]) { int temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } // search new index of saved active integer
  • 8. for (int i = 2; i <= a[0] + 1; i++) if (a[i] == active) { a[1] = i - 1; // write index of active integer break; } return; } void select(int a[]) { if (a[0]<=1) // selection illegal (one, or none elements in the list) return; // if the last integer is selected if (a[1] == a[0]) a[1] = 1; // select the first item in the array else a[1]++; // select next element in the list return; } void move_right(int a[]) { if (a[1]==a[0]) // move right is illegal (if the last integer is selected) return; int index = a[1]; // index of active element // swap active element (index in array[1]) with the next element int tmp = a[index+1]; a[index+1] = a[index+2]; a[index+2] = tmp; // correct index of active element a[1]++; return; } void move_left(int a[]) {
  • 9. // if the first integer is selected (array[1] = 1) or there is not selected element (array[1] = 0) if (a[1]<=1) // move left is illegal return; int index = a[1]; // index of active element // swap active element (index in array[1]) with the previous) int tmp = a[index]; a[index] = a[index+1]; a[index+1] = tmp; // correct index of active element a[1]--; return; } bool read_integer(istream& fin,int &number) { char symbol; number = 0; // initialize the number bool good_int; good_int = true; fin.get(symbol); if (symbol == ' ') fin.get(symbol); // clean if we have ' ' while (symbol !=' ') { if (isdigit(symbol)) // correction of the number number = number*10 + (symbol-'0'); else // user enters forbidden symbol good_int = false; fin.get(symbol); } return good_int; } I can input all positive intergal in this program, but I cannot input negative intergal. this program require that i just allow to input integer, and i need to input both negative and positive integer. what should I do? CAUsers Roder Desktopl Project
  • 10. 2.exe Insert" key "Delete" key F2" key Down Arrow" key 1. Insert 2. Delete 3. Sort 4. Select 5. Move Right "Right Arrow" key 6. Move Left 7. Exit "Left Arrow" key "F1" key -8-3 0 1 5 [12] 22 55 66 Enter inserted integer 8 You entered a wrong integer value Enter inserted integer: Solution The below function has an actual problem in reading negative numbers. The function is designed to check the weather the entered number is positive or not a number. It reads each and every character and checks weather it is digit or not if it's not a digit then function return false to the insert function. If you enter 345 as input then reads 3,4 and 5 individually and checks digit or not. In your case if you enter -8 function reads '-' and 8 individually. '-' is not a digit hence your function return false. Here just modify the function as given below bool read_integer(istream& fin,int &number) { char symbol; number = 0; // initialize the number bool good_int;
  • 11. good_int = true; fin.get(symbol); if (symbol == ' ') fin.get(symbol); // clean if we have ' ' int count=0; // count variable to check first char '-' or not int negNumber=0; while (symbol !=' ') { if(count==0) // checking first character weather enterd '-' or not { if(symbol=='-') // if it's negative number { fin.get(symbol); // next char negNumber=1; // detecting negative number count++; //incrementing count } else //if it's positive normal operation { if (isdigit(symbol)) // correction of the number number = number*10 + (symbol-'0'); else // user enters forbidden symbol good_int = false; fin.get(symbol); count++;
  • 12. } } else // for the next chars as usual operation { if (isdigit(symbol)) // correction of the number number = number*10 + (symbol-'0'); else // user enters forbidden symbol good_int = false; fin.get(symbol); count++; } } if(negNumber==1) number=number-(2*number); //loading negative number using basic formula ; i.e 10-2*10=-10; return good_int; }