In: Computer Science
Write the pseudocodes for these programs:
a)
#include <iostream>
using namespace std;
void print(int arr[],int n){
if(n==0)
return;
cout<<arr[n-1]<<" ";
print(arr,n-1);
}
int main()
{
int n;
cout<<"Enter the size of the array:";
cin>>n;
int arr[n];
int i;
cout<<"Enter the elements of the array:"<<endl;
for(i=0;i<n;i++){
cin >> arr[i];
}
cout<<"Displaying the elements of the array in reverse
order:"<<endl;
print(arr,n);
return 0;
}
b)
#include<iostream>
#include<fstream>
using namespace std;
//student structure
struct student
{
int id;
float gpa;
};
//node structure
struct node
{
student *data;
node *link;
};
//linklist class
class List
{
private:
node *head;
public:
List()
{
head=NULL;
}
//function to insert data into List
void Insert(student *S)
{
node *ptr=head;
node *temp=new node();
temp->data=S;
temp->link=NULL;
if(ptr==NULL)
{
head=temp;
}
else
{
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=temp;
}
}
//function to display data
void display()
{
node *ptr=head;
if(ptr==NULL)
{
cout<<"NO DATA EXISTS"<<endl;
}
else
{
while(ptr!=NULL)
{
student *s=ptr->data;
cout<<s->id<<" "<<s->gpa<<endl;
ptr=ptr->link;
}
}
}
};
int main()
{
ifstream infile;
string filename;
List L;
int id;
float gpa;
cout<<"Enter Filename : ";
cin>>filename;
infile.open(filename.c_str());
if(!infile)
{
cout<<"Unable to open file"<<endl;
return 0;
}
while(infile>>id>>gpa)
{
student *ptr=new student();
ptr->id=id;
ptr->gpa=gpa;
L.Insert(ptr);
}
//displaying data to Console
cout<<"STUDENT DATA"<<endl;
L.display();
}
a>
Function print to print the array in reverse order Pass In: integer array and its size n if n is equal to 0 then return to main function else print array element call: print with array of size n-1 Pass Out: nothing Endfunction
Function main
Pass In: nothing
Start
Declare an integer variable called n
INPUT n as a integer
Declare an integer array arr of size n
FOR i IN 1 TO n do
INPUT arr[i]
ENDFOR
call: Print
Pass Out: value zero to the operating system Endfunction
-------------------------------------------------------------------------------------------------
b>
start
declare a structure student with vaiables id as a integer and gpa as a float
declare a structure node with pointer named data using refToStudent ptr and pointer named link using refToNode ptr
declare class List
declare pointer named head data using refToNode ptr
set head to null
Function insert to insert the element in the student stuct Pass In: pointer named S using refToStudent ptr set node *ptr to head; create pointer named temp using refToNode ptr as a new node set temp->data to S; set temp->link to NULL; if ptr is equal to null then set head to temp else while ptr->link not equal to NULL set ptr to ptr->link endwhile set ptr->link to temp Endif Pass Out: nothing Endfunction
Function display to display the element in the student stuct
Pass In: nothing
set node *ptr to head
if ptr is equal to null then
print there is nothing to display
el;se
while ptr is not equal to NULL
set pointer names s data reftoStudent to ptr->data
print s->id and s->gpa
set ptr toptr->link
endwhile
endif
Pass Out: nothing
Endfunction
Function main
declare variable infile of type ifstream
decale filename as a string
declare L as List
declare id as a integer
declare gpa as a float
Input filename
if file is not opening then
print error message
else
while there is a data
create a pointer name ptr data refToStudent
set ptr->id to id
set ptr->gpa to gpa
call: insert
endwhile
call : display
Pass Out: nothing
Endmainfunction