Need help with some loops..

Ok.. this is a piece of code where the user input an array of numbers, then user choose its elements, and finally the average of those elements. I want the program to show again the entered elements and the value of each one, if the user wrong to type, something like a character or any sign instead of a number. So it works showing the elements from below but not the values that were given. How do I do that I refer to the line 41.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
.
..
else if(options == 4)
        {
            int mark[100], j, n, sum = 0, average;
            int temp4, status4;
            printf("\n\n\t 4. Average of n numbers using arrays");
            printf("\n\n\n\tInput number of elelments: ");
            status4 = scanf("%d", &n);
            while(status4 != 1)
            {
                while((temp4 = getchar()) != EOF && temp4 != '\n')
                {
                    printf("\n\n Input only numbers...");
                    Sleep(1500);
                    system("cls");
                    printf("\n\n\t 4. Average of n numbers using arrays");
                    printf("\n\n\n\tInput number of elelments: ");
                    status4 = scanf("%d", &n);
                }
            }
            system("cls");
            printf("\n\n\t 4. Average of n numbers using arrays");
            printf("\n\n\n\t%d elements were entered\n", n);
            for(j = 0; j < n; ++j)
            {
                printf("\n\tElement %d = ", j + 1);
                status4 = scanf("%d", &mark[j]);
                while(status4 != 1)
                {
                    while((temp4 = getchar()) != EOF && temp4 != '\n')
                    {
                        printf("\n\n Input only numbers...");
                        Sleep(1500);
                        system("cls");
                        printf("\n\n\t 4. Average of n numbers using arrays");
                        printf("\n\n\n\t%d elements were entered\n", n);
                        int k = j - 1;
                        for(k = 1; k < n; ++k)
                        {
                            printf("\n\tElement %d = %d\n", k, );
                        }
                        printf("\n\tElement %d = ", j + 1);
                        status4 = scanf("%d", &mark[j]);
                    }
                }
                sum += mark[j];
            }
            int options4;
            printf("\n 1. Average of the entered numbers"
                   "\n 2. Back to main menu"
                   "\n\n Select: ");
            status4 = scanf("%d", &options4);
            while(status4 != 1)
            {
                while((temp4 = getchar()) != EOF && temp4 != '\n')
                {
                    printf("\n\n Invalid option..");
                    Sleep(1500);
                    printf("\n\n Choose 1 or 2\n");
                    Sleep(1500);
                    system("cls");
                    printf("\n\n\t 4. Average of n numbers using arrays");
                    printf("\n\n\n\t%d elements were entered\n", n);
                    int k = j - 1;
                        for(k = 1; k <= n; ++k)
                        {
                            printf("\n\tElement %d = %d\n", k, );
                        }
                    printf("\n 1. Average of the entered numbers"
                           "\n 2. Back to main menu"
                           "\n\n Select: ");
                    status4 = scanf("%d", &options4);
                }
            }
            int i;
            for(i = 0; i < options4;)
            {
                if(options4 == 1)
                {
                    average = sum / n;
                    printf("\n Average of the entered numbers is %d\n", average);
                    Sleep(2500);
                    break;
                }
                else if(options4 == 2)
                {
                    printf("\n\n Back to main menu");
                    Sleep(2000);
                    system("cls");
                    menu();
                }
                else
                {
                    printf("\n\n Invalid option..");
                    Sleep(1500);
                    printf("\n\n Choose 1 or 2\n");
                    Sleep(1500);
                    system("cls");
                }
            }
        }
..
.
Last edited on
I do not understand your question.

are you intentionally writing C? or did you get this from somewhere?

that aside, I will try to answer what you asked in words for a moment..
if you want to pick say 10 numbers out of 100 in an array and average them, an easy way to do it is:
user inputs the selected numbers, you put them in a new array. This can be either by value or by index, however the user is telling you. Lets say it is by index, I think that is what you are asking:
for(int i = 0; i < number_selected; i++)
total += bigarray[user_array[i]]; //that is, if the user put in 2,4,8 then you add up [2], [4], and [8] etc.
average = total/(double)number_selected;
and to see what was entered, you can loop same way:
for(int i = 0; i < number_selected; i++)
//print user_array[i] and bigarray[user_array[i]] with words around them

The line 41 and 68 isn't complete missing the 3rd argument as I left it for purpose.. That's where I need help.. Thank you.
Yes I am writing C. So.. to understand me I'll make a fast example:

Output:

Average of n numbers using arrays

Input number of elelments: 3

3 elements were entered

Element 1 = 3
Element 2 = 4
Element 3 = p <- here where user is typing a letter so.. I want the user to enter again
the same element, but I even want the below elements 1 and 2 to
appear again with (the same value were entered) so.. if I don't put
the loop at line 41 the program will look like:

Output:

Average of n numbers using arrays

Input number of elelments: 3

3 elements were entered

Element 3 = <- Directly from the third element and not showing the elements were
entered first.

I did the loop.. and the elements are showing up but I'm not sure how to write the 3rd argument of ine 41

Let's say line 41 will be like this:

 
printf("\n\tElement %d = %d\n", k, k + n);


where k+n is not the write thing. But the elements are showing up with not the write values


Output:

Average of n numbers using arrays

Input number of elelments: 3

3 elements were entered

Element 1 = 5 <- Element 1 and 2 shows up as I use that loop but they don't have
Element 2 = 6 <- the right values..
Element 3 = 5

Well I don't know if anyone understand what do I want to do .. but is noting more then showing the right values of the elements that were entered first.

I use that loop to print again the elements of the array and their values that were entered after the user wrong type anything else out of numbers.
So.. if the user type the first element and is wrong typing the 2nd, I want my program to print the same thing including the first element with the right value and to start from the point where the user wrong type the second element and so on.

Its simply a matter of printing every time the elements were added first with their values and start from the next element until the end of the array where a menu shows up to make the average of those elements.
printf("\n\tElement %d = %d\n", k, mark[k]); //is this what you want?

if they mess up the input, you have to go again.
the common way to do that is to read into a string, check the string to see if it is valid, if it is convert it, and if not, make them do it again.
something like

for(index = 0; index < something; ) //don't increment here do it below
while(!right)
{
scanf(... into a string);
right = checkstring(str); //is the string entered valid? you need to code this: check all the letters to be digits (integer) or digits with a leading - sign, or more complex checks for doubles, etc.
if (right)
mark[index++] = atoi(str); //or atof for floats. the only way index changes is if it was valid...
}

Last edited on
Topic archived. No new replies allowed.