CSI 250L Lab 1 Solution, Spring 2012
American University of Science and Technology · Programming II Lab · 3 pages · Dr. Saeed Raheel · with solution
More CSI 250L files
- Lab 1 · Spring 2012LabView
- Lab 10 Solution · Spring 2012Lab · with solutionView
- Lab 10 · Spring 2012LabView
- Lab 11 Solution · Spring 2012Lab · with solutionView
- Lab 11 · Spring 2012LabView
- Lab 12 Solution · Spring 2012Lab · with solutionView
- Lab 12 · Spring 2012LabView
- Lab 13 Solution · Spring 2012Lab · with solutionView
Show the text of this lab
American University of Science & Technology
Department Of Computer Science
CSI 250L – Computer Programming-II Lab
Fall 2011/2012
Lab Work 1
Solutions prepared by: Dr. Saeed Raheel
Part 3 - Review Functions and arrays of integers
:
Write a program to allow the user to input N positive integers and store them in an array,
arr, of 100 cells.(let N be the first input by the user)
The program should print the content of arr sorted in ascending order.
#include <iostream>
using namespace std;
void printArray(int a[], int size)
{
for (int i=0;i<size;++i)
cout << a[i] << " ";
cout<<endl;
}
void sortAscending(int a[], int size)
{
int temp;
for (int i=0;i<size;++i)
for (int j=0;j<size-1;++j)
{
if (a[j]>a[j+1])
{
temp = a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
int main()
{
int N;
int arr[100];
cout << "Please enter the number of values to be entered : ";
cin >> N;
cout << "Please enter " << N << " numbers :" << endl;
for (int i=0;i<N;++i)
cin >> arr[i];
sortAscending(arr, N);
cout << "The array elements sorted in ascending order : " <<
endl;
printArray(arr, N);
system("pause");
return 0;
}
Part 4 - Review Functions and arrays of characters
:
Write a program that inputs a word into an array S1. The program should store in an array
S2 the reverse of S1, and in S3 the content of S1 converted to capital letters.
#include <iostream>
using namespace std;
int getArraySize(char a[])
{
int i;
for (i=0;a[i]!='\0';++i);
return i;
}
void printArray(char a[])
{
for (int i=0;a[i]!='\0';++i)
cout << a[i];
cout<<endl;
}
void reverse(char a[], char b[])
{
int size = getArraySize(a);
for (int i=0;a[i]!='\0';++i)
b[i] = a[size-i-1];
b[size]='\0';
}
void capitalize(char a[], char b[])
{
int i;
for (i=0;a[i]!='\0';++i)
if (a[i]>='a' && a[i] <='z')
b[i] = ('A' + (a[i]-'a'));
else
b[i] = a[i];
b[i] = '\0';
}
int main()
{
const int size = 25;
char s1[size], s2[size], s3[size];
cout << "Please enter a word : ";
cin >> s1;
reverse(s1, s2);
cout << "The word in reverse order : ";
printArray(s2);
cout << endl;
capitalize(s1, s3);
cout << "The word in capital letters : ";
printArray(s3);
cout << endl;
system("pause");
return 0;
}