Question

In: Computer Science

Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){...

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();
}

Solutions

Expert Solution

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

  

  


Related Solutions

#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ;...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ; int p = abs(-90); cout << ::p + p - var << endl; system("pause"); } double var = 5.5;
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT