School Commit Init
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
#include<stdio.h>
|
||||
|
||||
typedef struct{
|
||||
int length;
|
||||
int elems[100];
|
||||
}vector;
|
||||
|
||||
vector read(){
|
||||
/*
|
||||
:input:
|
||||
|
||||
:output:
|
||||
vector v
|
||||
|
||||
:description:
|
||||
return a vector of integer numbers read from the keyboard
|
||||
*/
|
||||
vector v;
|
||||
v.length = 0;
|
||||
while (scanf("%d", &v.elems[v.length]) && v.elems[v.length] != 0)
|
||||
{
|
||||
v.length++;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
void printVector(vector v,int startI,int endI){
|
||||
/*
|
||||
:input:
|
||||
vector v
|
||||
int startI
|
||||
int endI
|
||||
:output:
|
||||
void
|
||||
|
||||
:description:
|
||||
print the elements of the vector v from startI (inclusive) to endI (exclusive)
|
||||
*/
|
||||
for (int i = startI; i < endI; i++)
|
||||
{
|
||||
printf("%d ", v.elems[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void printInstructions(){
|
||||
printf("1. Read a vector\n");
|
||||
printf("2. Print the sum of a sequence of numbers\n");
|
||||
printf("3. Print the longest contiguous subsequence of equal numbers\n");
|
||||
printf("0. Exit\n");
|
||||
}
|
||||
|
||||
int sum_of_vector(vector v){
|
||||
/*
|
||||
:input:
|
||||
vector v
|
||||
:output:
|
||||
int sum
|
||||
|
||||
:description:
|
||||
return the sum of the elements of the vector v
|
||||
*/
|
||||
int sum = 0;
|
||||
for (int i = 0; i < v.length; i++)
|
||||
{
|
||||
sum += v.elems[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
void waitForUserInput(){
|
||||
vector v;
|
||||
v.length = 0;
|
||||
while(1){
|
||||
printInstructions();
|
||||
int option;
|
||||
scanf("%d", &option);
|
||||
switch (option)
|
||||
{
|
||||
case 1:
|
||||
v=read();
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
case 3:
|
||||
break;
|
||||
case 0:
|
||||
return;
|
||||
default:
|
||||
printf("Invalid option\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
waitForUserInput();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user