1 sequences and sampling Suppose we went to sample the xaxis
1 sequences and sampling. Suppose we went to sample the x-axis from Xmin to Xmax using a step size of step
A)Draw a picture of what is going on.
B) Write a expression for n the total number of samples involved (in terms of Xmin, Xmax and step)
C) Write out the sequence of x-samples
D) Write a direct and general expression for xi that captures the sequence
E) Write a recursive expression for the sequence
F) Write a program to compute and store the x-samples over the range -5x5 using a step size of 0.1 do everything in main ()
2 . We talked about the following string functions that are available in C (as long as you include string.h):
int strlen(char str[])
void strcpy(char str1[], char str2[])
void strcat(char str1[], str2[])
Write your own versions of these functions; for example: int paul_strlen(int char str[]). Hint: for your version of the strlen function, start at the first character in the array and keep counting until you find the ‘\\0’ character (use a while loop for this). Note: Use your version of the strlen function in the strcpy and strcat functions.
9. We want to insert a number into an array.
(a) Formulate the problem mathematically with two sequences: x and y. (b) Write a function of the form:
insertNumIntoArray(int n, int array[], int num, int index)
The function inserts num into the array at the specified index. The rest of the array then follows. For example, if num = 9 and index = 3 and array = [7 2 8 8 3 1 2] then the function will produce:
array = [7 2 8 9 8 3 1 2]
Note: assume that array is properly dimensioned to have at least 1 extra space for storage.
10. Repeat #2 by for the delete operation; that is, we want to delete a single element (at a specified index) from an array; for example, suppose index = 3 and array = [50 70 10 90 60 20], then the result will be
array: [50 70 10 60 20]
11. Repeat #2 by for an insert operation where we are inserting several values into the array. The function should be of the form:
int insertArrayIntoArray(int n, int inArray[],
int nInsert, int insertArray[], int outArray[], int index)
The dimension of outArray is returned (explicitly). For example:
inArrayarray: [7 2 8 6 3 9]
insertArray: [50 60 70]
index: 2
outArray: [7 2 50 60 70 8 6 3 9]
Assume that outArray is large enough to hold all n + nInsert values.
Solution
#include<stdio.h>
//Simulates strlen() library function
int paul_strlen(char str[])
{
int l;
for(l = 0; str[l] != \'\\0\'; l++) ;
return l;
}
//Simulates strcpy() library function
void paul_strcpy(char str1[], char str2[])
{
int c;
for(c = 0; str1[c] != \'\\0\'; c++)
str2[c] = str1[c];
str2[c] = \'\\0\';
printf(\"\ Original String: %s\", str1);
printf(\"\ Copied String: %s\", str2);
}
//Simulates strcat() library function
void paul_strcat(char str1[], char str2[])
{
int i, j;
for(i = 0; str1[i] != \'\\0\'; i++) ;
for (j = 0; str2[j] != \'\\0\'; i++, j++)
{
str1[i] = str2[j];
}
str1[i] = \'\\0\';
printf(\"\ Concatenated String: %s\", str1);
}
int main()
{
char data1[20], data2[20];
printf(\"\ Enter a string: \");
gets(data1);
printf(\"\ Length of the String: %d\", paul_strlen(data1));
printf(\"\ \ Enter a string: \");
gets(data1);
paul_strcpy(data1, data2);
printf(\"\ \ Enter a string1: \");
gets(data1);
printf(\"\ \ Enter a string2: \");
gets(data2);
paul_strcat(data1, data2);
}
Output:
Enter a string: pyari
Length of the String: 5
Enter a string: Pyari sahu
Original String: Pyari sahu
Copied String: Pyari sahu
Enter a string1: pyari
Enter a string2: mohan
Concatenated String: pyarimohan
9. We want to insert a number into an array.
Answer:
#include<stdio.h>
//Insert a number at specified location
void insertNumIntoArray(int n, int array[], int num, int index)
{
int c;
//Creates a place for insert
for (c = n - 1; c >= index; c--)
array[c + 1] = array[c];
//Stores the number at specified location
array[index] = num;
printf(\"Resultant array is\ \");
for (c = 0; c <= n; c++)
printf(\"%d\ \", array[c]);
}
int main()
{
int array[20];
int n, c, num, index;
printf(\"Enter number of elements in array\ \");
scanf(\"%d\", &n);
printf(\"Enter %d elements\ \", n);
for (c = 0; c < n; c++)
scanf(\"%d\", &array[c]);
printf(\"Enter the location where you wish to insert an element\ \");
scanf(\"%d\", &index);
printf(\"Enter the value to insert\ \");
scanf(\"%d\", &num);
insertNumIntoArray(n, array, num, index);
}
Output:
Enter number of elements in array
7
Enter 7 elements
7
2
8
8
3
1
2
Enter the location where you wish to insert an element
3
Enter the value to insert
9
Resultant array is
7
2
8
9
8
3
1
2
10. Repeat #2 by for the delete operation; that is, we want to delete a single element (at a specified index) from an array; for example, suppose index = 3 and array = [50 70 10 90 60 20], then the result will be
Answer:
#include <stdio.h>
//Delete an item from a specified position
void deleteNumFromArray(int n, int array[], int index)
{
int c;
//Validates for deletion
if ( index >= n+1 )
printf(\"Deletion not possible.\ \");
else
{
//Moves the elements
for ( c = index ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf(\"Resultant array is\ \");
for( c = 0 ; c < n - 1 ; c++ )
printf(\"%d\ \", array[c]);
}
}
int main()
{
int array[100], index, c, n;
printf(\"Enter number of elements in array\ \");
scanf(\"%d\", &n);
printf(\"Enter %d elements\ \", n);
for ( c = 0 ; c < n ; c++ )
scanf(\"%d\", &array[c]);
printf(\"Enter the location where you wish to delete element\ \");
scanf(\"%d\", &index);
deleteNumFromArray(n, array, index);
return 0;
}
Output:
Enter number of elements in array
6
Enter 6 elements
50
70
10
90
60
20
Enter the location where you wish to delete element
3
Resultant array is
50
70
10
60
20
11. Repeat #2 by for an insert operation where we are inserting several values into the array. The function should be of the form:
int insertArrayIntoArray(int n, int inArray[],
int nInsert, int insertArray[], int outArray[], int index)
The dimension of outArray is returned (explicitly). For example:
inArrayarray: [7 2 8 6 3 9]
insertArray: [50 60 70]
index: 2
outArray: [7 2 50 60 70 8 6 3 9]
Assume that outArray is large enough to hold all n + nInsert values.
Answer:
#include<stdio.h>
//Insert an array within another array at a specified index
int insertArrayIntoArray(int n, int inArray[], int nInsert, int insertArray[], int outArray[], int index)
{
int c, d = 0;
for(d = 0; d < n; d++)
outArray[d] = inArray[d];
//Loops till end of the second array
for(d = 0; d < nInsert; d++)
{
//Searche for the index position and swaps the data to next position
for (c = n - 1; c >= index; c--)
{
outArray[c + 1] = outArray[c];
}
//Inserts the second array data to the output array at specified position
outArray[index] = insertArray[d];
index++; //Increase the index
n++; //Increse the length
}
n = n + nInsert;
}
//Accept array data
void acc(int arr[], int n)
{
for(int c = 0; c < n; c++)
{
printf(\"\ Enter element %d \", c+1);
fflush(stdin);
scanf(\"%d\",&arr[c]);
}
}
//Display array data
void disp(int arr[], int n)
{
for(int c = 0; c < n; c++)
{
printf(\"%4d\", arr[c]);
}
}
int main()
{
int fsize, ssize, pos;
int first[50], second[50], third[100];
printf(\"\ Enter first Array Size: \");
scanf(\"%d\", &fsize);
printf(\"\ Enter second Array Size: \");
scanf(\"%d\", &ssize);
printf(\"\ Enter data for First Array: \");
acc(first, fsize);
printf(\"\ Enter data for Second Array: \");
acc(second, ssize);
printf(\"\ Enter the Index position: \");
fflush(stdin);
scanf(\"%d\", &pos);
printf(\"\ Frist Array: \ \");
disp(first, fsize);
printf(\"\ Second Array: \ \");
disp(second, ssize);
insertArrayIntoArray(fsize, first, ssize, second, third, pos);
printf(\"\ Merged Array: \ \");
disp(third, (fsize+ssize));
}
Output:
Enter first Array Size: 6
Enter second Array Size: 3
Enter data for First Array:
Enter element 1 7
Enter element 2 2
Enter element 3 8
Enter element 4 6
Enter element 5 3
Enter element 6 9
Enter data for Second Array:
Enter element 1 50
Enter element 2 60
Enter element 3 70
Enter the Index position: 2
Frist Array:
7 2 8 6 3 9
Second Array:
50 60 70
Merged Array:
7 2 50 60 70 8 6 3 9





