lab8
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
#include "func.h"
|
||||
|
||||
void find(int type, int size, int *massive){
|
||||
|
||||
int x,k,m,sum;
|
||||
switch(type){
|
||||
|
||||
case 6:
|
||||
case 8:
|
||||
for (int i = size - 1; i>=0;i--)
|
||||
if(massive[i]>0){
|
||||
printf("last positive int: ");
|
||||
printf("[%3d]\t{%p}\t%d\n",i,&massive[i],massive[i]);
|
||||
break;
|
||||
}
|
||||
printf("positive int is not found.\n");
|
||||
break;
|
||||
|
||||
|
||||
case 7:
|
||||
case 9:
|
||||
for (int i = size; i>=0;i--)
|
||||
if(massive[i]<0){
|
||||
printf("last negative int: ");
|
||||
printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
|
||||
break;
|
||||
}
|
||||
printf("negative int is not found.\n");
|
||||
break;
|
||||
|
||||
|
||||
case 14:
|
||||
x=k=0;
|
||||
printf("find all modules of k\nInput: ");
|
||||
while(scanf(" %d",&k)!=1||k>size-1){
|
||||
if(k>size-1)printf("k shood be lower than %d",size);
|
||||
if(flush())return;
|
||||
}
|
||||
if(flush())return;
|
||||
for(int i = 0; i<size;i++){
|
||||
if(massive[i]%k==0){
|
||||
printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);x++;}
|
||||
}
|
||||
printf("Num of modules - %d",x);
|
||||
break;
|
||||
|
||||
|
||||
case 15:
|
||||
x=0;k=1;m=0;
|
||||
for(int i = 0;i<size-1;i++){
|
||||
for(int j = i+1;j<size-1;j++){
|
||||
if(massive[i] == massive[j]){
|
||||
k=i;
|
||||
m=j;
|
||||
break;}
|
||||
}
|
||||
if (k==m)break;
|
||||
}
|
||||
for(int i = k;i<m;i++)
|
||||
x++;
|
||||
|
||||
if(k==m)
|
||||
printf("elements between [%d] and [%d] - %d",k,m,x);
|
||||
else printf("Error same elements not found!\n");
|
||||
break;
|
||||
|
||||
case 19:
|
||||
case 21:
|
||||
case 23:
|
||||
sum=0;
|
||||
printf("find all int that lower k\nInput: ");
|
||||
while(scanf(" %d",&k)!=1){
|
||||
if(flush())return;
|
||||
}
|
||||
if(flush())return;
|
||||
for(int i = 0; i<size;i++){
|
||||
if(k<massive[i]){
|
||||
if(type != 21)
|
||||
printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
|
||||
x++;
|
||||
sum+=massive[i];
|
||||
}
|
||||
}
|
||||
if(type == 21)printf("summ - %d\n",sum);
|
||||
break;
|
||||
|
||||
case 20:
|
||||
case 22:
|
||||
case 24:
|
||||
sum=0;
|
||||
printf("find all int higher lower k\nInput: ");
|
||||
while(scanf(" %d",&k)!=1){
|
||||
if(flush())return;
|
||||
}
|
||||
if(flush())return;
|
||||
for(int i = 0; i<size;i++){
|
||||
if(k>massive[i]){
|
||||
if(type != 21)
|
||||
printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
|
||||
x++;
|
||||
sum+=massive[i];
|
||||
}
|
||||
}
|
||||
if(type == 21)printf("summ - %d\n",sum);
|
||||
break;
|
||||
|
||||
case 25:
|
||||
case 27:
|
||||
case 29:
|
||||
sum=0;
|
||||
printf("find all int higher lower mean\nInput: ");
|
||||
for(int i = 0; i<size;i++){
|
||||
sum +=massive[i];
|
||||
x++;
|
||||
}
|
||||
k = sum/x;
|
||||
x = sum = 0;
|
||||
for(int i = 0; i<size;i++){
|
||||
if(k>massive[i]){
|
||||
if(type != 21)
|
||||
printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
|
||||
x++;
|
||||
sum+=massive[i];
|
||||
}
|
||||
}
|
||||
if(type == 29)printf("summ - %d\n",sum);
|
||||
break;
|
||||
|
||||
case 26:
|
||||
case 28:
|
||||
case 30:
|
||||
sum=0;
|
||||
printf("find all int higher lower mean\nInput: ");
|
||||
for(int i = 0; i<size;i++){
|
||||
sum +=massive[i];
|
||||
x++;
|
||||
}
|
||||
k = sum/x;
|
||||
x = sum = 0;
|
||||
for(int i = 0; i<size;i++){
|
||||
if(k<massive[i]){
|
||||
if(type != 21)
|
||||
printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
|
||||
x++;
|
||||
sum+=massive[i];
|
||||
}
|
||||
}
|
||||
if(type == 30)printf("summ - %d\n",sum);
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef ARRAY_OPS_H
|
||||
#define ARRAY_OPS_H
|
||||
|
||||
#include <stdio.h>
|
||||
#define MAXMASSIVESIZE 10000
|
||||
|
||||
|
||||
void setmass(int *massSize);
|
||||
int fillmass(int massSize, int *massive);
|
||||
void viewmass(int massSize, int *massive);
|
||||
void selfunc(int size, int *massive);
|
||||
int flush(void);
|
||||
|
||||
void range(int type,int size, int *massive);
|
||||
void find(int type,int size, int *massive);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,147 @@
|
||||
#include "func.h"
|
||||
|
||||
void setmass(int *massSize){
|
||||
printf("\t\tSetMassiveSize\n");
|
||||
printf("Set Massive Size - input integer\n");
|
||||
printf("Exit from this menu - input 0\n\n\n");
|
||||
printf("Input: ");
|
||||
int n;
|
||||
while(scanf(" %d",&n)!=1||n>MAXMASSIVESIZE){
|
||||
if(flush())return;
|
||||
flush();
|
||||
if(n>10000)printf("Input cannot be >%d",MAXMASSIVESIZE);
|
||||
printf("Input Err\nInput: ");
|
||||
}
|
||||
|
||||
flush();
|
||||
|
||||
if(n==0)
|
||||
return;
|
||||
else
|
||||
*massSize = n;
|
||||
return;
|
||||
}
|
||||
|
||||
int fillmass(int size, int *massive){
|
||||
printf("\t\tFill Massive\n");
|
||||
printf("You can devide elements with space or enter.\n");
|
||||
printf("You also can decide to from which element start with e.\n");
|
||||
printf("To exit input g.\n\n\n");
|
||||
printf("Input: ");
|
||||
|
||||
|
||||
int x, i, e1,e = 0, p=-1, minus = 0;
|
||||
|
||||
for(i = 0;i < size;i++){
|
||||
e1 = e = massive[i] = 0;
|
||||
while(((x = getchar()) != '\n' && x !=' '
|
||||
&& x != EOF && x != 4)
|
||||
&& ((x>='0' && x<='9')
|
||||
|| x == 'e' || (x == '-' && e==0 && massive[i]==0)))
|
||||
{
|
||||
if (x == 'e')e1=1;
|
||||
else if (x == '-')minus=1;
|
||||
|
||||
else if (e1) e = (e*10)+(x-'0');
|
||||
else massive[i] = (massive[i]*10) + (x-'0');
|
||||
}
|
||||
|
||||
if(x == EOF || x == 3){return 0;}
|
||||
if(e1&&minus)minus=0;
|
||||
if(minus && (x == '\n' || x == ' ')){minus = 0; massive[i]*=-1;}
|
||||
if(e>size) {printf("e is out of range.\n");i=p;}
|
||||
else if(e1) {i = e - 1;putchar('\n');}
|
||||
if(x == 'g'){return 1;}
|
||||
if((x<='0' && x>='9')||x==' '||x=='\n'||x<=32);
|
||||
else {
|
||||
while((x=getchar())!='\n'&&x!=' '&&x!=EOF&&x!=4);
|
||||
i--;
|
||||
printf("ErrInput\n");}
|
||||
if(p!=i&&e1!=1)
|
||||
printf("Massive - [%d]\t%d\n",i,massive[i]);
|
||||
p=i;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void viewmass(int size, int *massive){
|
||||
printf("\t\tView Massive\n");
|
||||
for(int i = 0; i < size; i++){
|
||||
printf(" | [%6d]\t%p\t%d\n",i,&massive[i],massive[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void selfunc(int size, int *massive){
|
||||
printf("\t\tFunctions\t(my - 7|16|24)\n");
|
||||
|
||||
printf("\tRange Operations\n");
|
||||
printf("2. Find Max Range\n");
|
||||
printf("3. Find Min Range\n");
|
||||
printf("10. Sum Range\n");
|
||||
printf("11. Average Range\n");
|
||||
printf("18. Diff Min Max Range\n\n");
|
||||
|
||||
printf("\tValue Search\n");
|
||||
printf("6. Find Last Positive\n");
|
||||
printf("7. Find Last Negative\n\n");
|
||||
|
||||
printf("\tIndex Search\n");
|
||||
printf("8. Find Last Positive Index\n");
|
||||
printf("9. Find Last Negative Index\n");
|
||||
printf("13. Find Indices Between\n");
|
||||
printf("17. Find Indices Value Range\n\n");
|
||||
|
||||
printf("\tValue Filtering\n");
|
||||
printf("4. Find Indices Above\n");
|
||||
printf("5. Find Indices Below\n");
|
||||
printf("19. Count Below Value\n");
|
||||
printf("20. Count Above Value\n");
|
||||
printf("21. Sum Below Value\n");
|
||||
printf("22. Sum Above Value\n");
|
||||
printf("23. Find Indices Below Value\n");
|
||||
printf("24. Find Indices Above Value\n\n");
|
||||
|
||||
printf("\tAverage Comparisons\n");
|
||||
printf("25. Count Above Average\n");
|
||||
printf("26. Count Below Average\n");
|
||||
printf("27. Find Indices Below Average\n");
|
||||
printf("28. Find Indices Above Average\n");
|
||||
printf("29. Sum Below Average\n");
|
||||
printf("30. Sum Above Average\n\n");
|
||||
|
||||
printf("\tSpecial Conditions\n");
|
||||
printf("1. Find Min Max\n");
|
||||
printf("12. Average Range\n");
|
||||
printf("14. Count Multiples\n");
|
||||
printf("15. Count Between Equals\n");
|
||||
printf("16. Sum Value Range\n\n");
|
||||
|
||||
printf("\tExit - 0\n");
|
||||
|
||||
int n;
|
||||
while(scanf(" %d",&n)!=0){
|
||||
if(flush())return;
|
||||
if (n == 0) return;
|
||||
if (n == 1 || n == 2 || n == 3 || n == 4 || n == 5 ||
|
||||
n == 10 || n == 11 || n == 12 || n == 13 ||
|
||||
n == 16 || n == 17 || n == 18) {
|
||||
range(n, size, massive);
|
||||
}else if ((n >= 6 && n <= 9) || n == 14 || n == 15 ||
|
||||
(n >= 19 && n <= 30)) {
|
||||
find(n, size, massive);
|
||||
} else {
|
||||
printf("Function number %d invalid.\n", n);
|
||||
}
|
||||
printf("Input: ");
|
||||
}
|
||||
}
|
||||
|
||||
int flush(void){
|
||||
int c;
|
||||
while ((c = getchar()) != '\n' && c != EOF)
|
||||
if (c == 4 || c == 3) return 1;
|
||||
if (c == EOF) return 1;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
#include "func.h"
|
||||
|
||||
void range(int type,int size, int *massive){
|
||||
int x, k, m;
|
||||
|
||||
switch(type){
|
||||
case 1:
|
||||
k = massive[0];
|
||||
m = massive[0];
|
||||
printf("\tFinding max and min int in massive.\n");
|
||||
|
||||
for(int i = 0;i<size;i++){
|
||||
m = (m>massive[i])?m:massive[i];
|
||||
k = (k<massive[i])?k:massive[i];
|
||||
}
|
||||
printf("Max massive int is %d.\n",m);
|
||||
printf("Min massive int is %d.\n\n",k);
|
||||
break;
|
||||
|
||||
|
||||
|
||||
case 2:
|
||||
printf("\tEnter Range where find max int.\n");
|
||||
printf("Input: min max\nInput: ");
|
||||
while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){
|
||||
if(k<0)printf("min cannot be below 0!\n");
|
||||
if(m>size)printf("max cannot be bigger %d!\n",size);
|
||||
if(k>m)printf("min cannot be bigger than max!\n");
|
||||
if(flush())return;
|
||||
printf("Input: ");
|
||||
}
|
||||
if(flush())return;
|
||||
x=massive[k];
|
||||
for(int i = k;i<=m;i++){
|
||||
x = (x>massive[i])?x:massive[i];
|
||||
}
|
||||
printf("Max massive int betveen [%d] and [%d] is %d.\n\n",k,m,x);
|
||||
break;
|
||||
|
||||
|
||||
case 3:
|
||||
printf("\tEnter Range where find min int.\n");
|
||||
printf("Input: min max\nInput: ");
|
||||
while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1 || k>m){
|
||||
if(k<0)printf("min cannot be below 0!\n");
|
||||
if(m>size)printf("max cannot be bigger %d!\n",size);
|
||||
if(k>m)printf("min cannot be bigger than max!\n");
|
||||
if(flush())return;
|
||||
}
|
||||
if(flush())return;
|
||||
x = massive[k];
|
||||
for(int i = k;i<=m;i++){
|
||||
x = (x<massive[i])?x:massive[i];
|
||||
}
|
||||
printf("Min massive int betveen [%d] and [%d] is %d.\n\n",k,m,x);
|
||||
return;
|
||||
|
||||
|
||||
case 4:
|
||||
printf("\tInput int to find all higher int.\n");
|
||||
printf("\nInput: ");
|
||||
while(scanf(" %d", &k)!=1)
|
||||
if(flush())return;
|
||||
if(flush())return;
|
||||
x = 0;
|
||||
for(int i = 0;i<size;i++){
|
||||
if (massive[i]>k){printf("[%3d]\t{%p}\n",i,&massive[i]);x++;}
|
||||
}
|
||||
if(x==0)printf("int > %d not found.",k);
|
||||
break;
|
||||
|
||||
|
||||
case 5:
|
||||
printf("\tInput int to find all lower int.\n");
|
||||
printf("\nInput: ");
|
||||
while(scanf(" %d", &k)!=1)
|
||||
if(flush())return;
|
||||
if(flush())return;
|
||||
x = 0;
|
||||
for(int i = 0;i<size;i++){
|
||||
if (massive[i]<k){printf("[%3d]\t{%p}\n",i,&massive[i]);x++;}
|
||||
}
|
||||
if(x==0)printf("int < %d not found.",k);
|
||||
break;
|
||||
|
||||
case 16:
|
||||
case 10:
|
||||
printf("\tEnter Range.\n");
|
||||
printf("Input: min max\nInput: ");
|
||||
while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){
|
||||
if(k<0)printf("min cannot be below 0!\n");
|
||||
if(m>size)printf("max cannot be bigger %d!\n",size);
|
||||
if(k>m)printf("min cannot be bigger than max!\n");
|
||||
if(flush())return;
|
||||
printf("Input: ");
|
||||
}
|
||||
if(flush())return;
|
||||
x=massive[k];
|
||||
for(int i = k;i<=m;i++){
|
||||
x += massive[i];
|
||||
}
|
||||
printf("Sum betveen [%d] and [%d] is %d.\n\n",k,m,x);
|
||||
break;
|
||||
|
||||
case 11:
|
||||
case 12:
|
||||
printf("\tEnter Range where find arithmetic mean\\n.\n");
|
||||
printf("Input: min max\nInput: ");
|
||||
while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){
|
||||
if(k<0)printf("min cannot be below 0!\n");
|
||||
if(m>size)printf("max cannot be bigger %d!\n",size);
|
||||
if(k>m)printf("min cannot be bigger than max!\n");
|
||||
if(flush())return;
|
||||
printf("Input: ");
|
||||
}
|
||||
if(flush())return;
|
||||
x=massive[k];int c=0;
|
||||
for(int i = k;i<=m;i++){
|
||||
x += massive[i];
|
||||
c++;
|
||||
}
|
||||
x /= c;
|
||||
printf("arithmetic mean betveen [%d] and [%d] is %d.\n\n",k,m,x);
|
||||
break;
|
||||
|
||||
|
||||
case 13:
|
||||
k = massive[0];
|
||||
m = massive[0];
|
||||
printf("\tFinding max and min int in massive.\n");
|
||||
|
||||
for(int i = 0;i<size;i++){
|
||||
m = (m>massive[i])?m:massive[i];
|
||||
k = (k<massive[i])?k:massive[i];
|
||||
}
|
||||
for(int i = k;i<m;i++){
|
||||
printf("[%3d]\t{%p}\n",i,&massive[i]);
|
||||
}
|
||||
break;
|
||||
|
||||
case 17:
|
||||
printf("\tInput Range to find index in between\\n.\n");
|
||||
printf("Input: min max\nInput: ");
|
||||
while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){
|
||||
if(k<0)printf("min cannot be below 0!\n");
|
||||
if(m>size)printf("max cannot be bigger %d!\n",size);
|
||||
if(k>m)printf("min cannot be bigger than max!\n");
|
||||
if(flush())return;
|
||||
printf("Input: ");
|
||||
}
|
||||
if(flush())return;
|
||||
for(int i = k;i<m;i++){
|
||||
printf("[%3d]\t{%p}\n",i,&massive[i]);
|
||||
}
|
||||
break;
|
||||
|
||||
case 18:
|
||||
printf("\tInput Range to find diff between min and max\\n.\n");
|
||||
printf("Input: min max\nInput: ");
|
||||
while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){
|
||||
if(k<0)printf("min cannot be below 0!\n");
|
||||
if(m>size)printf("max cannot be bigger %d!\n",size);
|
||||
if(k>m)printf("min cannot be bigger than max!\n");
|
||||
if(flush())return;
|
||||
printf("Input: ");
|
||||
}
|
||||
if(flush())return;
|
||||
k = massive[k];
|
||||
m = massive[k];
|
||||
for(int i = k;i<m;i++){
|
||||
m = (m>massive[i])?m:massive[i];
|
||||
k = (k<massive[i])?k:massive[i];
|
||||
}
|
||||
printf("min - %d\tmax - %d\tdiff - %d\n",k,m,m-k);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
Reference in New Issue
Block a user