help

Hi everyone!
can anyone helps me on this lab on c++ programming.

You are to write a function int readDouble (double x[]) which will read (at most 50 ) doubles from the keyboard until a non-double or zero is inputted from the keyboard. You are to store each double in the array List which you will define in the main. In readDouble you need to count the number of doubles that have been inputted and return this value.

Thanks.
closed account (48T7M4Gy)
1. write a function int readDouble (double x[]) which will read (at most 50 ) doubles from the keyboard until a non-double or zero is inputted from the keyboard.

2. store each double in the array List which you will define in the (sic) main.

3. In readDouble count the number of doubles that have been inputted and return this value.

There you go champ. Have a crack at part 1 and let us see your code and any associated problems.
closed account (48T7M4Gy)
BTW please don't double post.
http://www.cplusplus.com/forum/general/177396/
alright this is my code but still wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
double List[50]={0};
int size;
size = readDouble (List);
printf("\n Inverted sum is %lf\n", myInvSum (List, size));
cout << "\n Array before sorting: ";
myPrint (List, size); // call all the functions one by one
Sort (List, size);
cout << "\n Array after sorting: ";
myPrint (List, size);
specialSort (List, size);
cout << "\n Array after special sorting: ";
myPrint (List, size);
printf("\n Inverted sum is %lf\n", myInvSum (List, size));



//double array inverse sum and print
int readDouble(double list[50]) {
int size = 1, checked;
cout << "\n Enter Array size (maximum 50) ";
cin >> size;
while (size > 50 ) {
cout << "\n Array size cannot be >50, please enter array size less than 50 ";
cin >> size;
} // end while
double item; // array elelment item
for (int i=0; i<size; i++) {
cout << " Enter a double value as array element, zero or non double will stop ";
cin >> item;
if ( item == 0 ) break; // stop upon zero input
// if ( ! (cin >> item) ) { cin.clear(); cin.ignore(1000, '\n'); break ; }
// stop upon non double input
if ( ! cin.eof()) {
checked = cin.peek();
if ( checked == 10 && cin.good())
list[i] = item; // store in to the array
else {
cout << "\n Entered a non double, stopping now ";
break;
} // end else
// end if cin eof
// end for
return size;

}
// end readDbl

double myInvSum (double list[50], int size) {
//This function will return the sum 1/x[0]+1/x[1]+...+1/x[size-1]
double sum = 0.0;
for (int i=0; i<size; i++)
sum += 1/list[i]; // invert the number and then add
return sum;

} // end inv sum

void myPrint(double list[50], int size) {
cout << "\n The array elements are: ";
for (int i=0; i<size; i++)
cout << list[i] << " , "; // simply print out to monitor or standard out
} // end myPrint

void Sort (double list[50], int size) { // in ascending order
int j;
double temp;
for (int i=0; i<=size-1; i++) // bubble sort the double array
for (j=i+1; j<size; j++) // inner for loop for j
if ( list[i] > list[j] ) { // then swap
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}



void specialSort (double list[50], int size) {
double d1,d2;
int int1, int2, hdi, hdj;
int j;
double temp;
for (int i=0; i<=size-1; i++)
for (j=i+1; j<size; j++) {

d1 = list[i]; // say d1 = 2.7345
d2 = d1 * 1000.0; // d2 = 2734.5
int1 = d2 / 10; // integer division will give you int1 = 2734.5 / 10 = 273
int2 = int1 / 10 ; // int2 = 273 / 10 = 27
hdi = int1 - int2*10 ; // hd = 273 - 27*10 = 273 - 270 = 3

// and then get hdj next in the same way
d1 = list[j]; // say d1 = 2.6530
d2 = d1 * 1000.0; // d2 = 2653.0
int1 = d2 / 10; // integer division will give you int1 = 2653.0 / 10 = 265
int2 = int1 / 10 ; // int2 = 265 / 10 = 26
hdj = int1 - int2*10 ; // hd = 265 - 26*10 = 265 - 260 = 5

if ( hdi > hdj) { // then swap
temp = list[i];
list[i] = list[j];
list[j] = temp;
{
{
}

return 0;
}
Don't get confused. What you're doing is not C++ programming. It's a love child between C and a bad-style C++.
Hey bodyguard...

I don't mean to offend, or be rude. Dude...

You have no indentation style. This makes your code very hard to read.
List is a type of container, it makes for a horrid variable name.
size is a key word in container classes and makes for a horrid variable name.
There is no prototype for int readdouble(List)... the compiler can't see it from where you called it.
1
2
3
{
{ 
}
What's that at the end of the file ?

C:\Crap\main.cpp: In function 'int main()':
C:\Crap\main.cpp:9:24: error: 'readDouble' was not declared in this scope
C:\Crap\main.cpp:10:56: error: 'myInvSum' was not declared in this scope
C:\Crap\main.cpp:11:1: error: 'cout' was not declared in this scope
C:\Crap\main.cpp:12:20: error: 'myPrint' was not declared in this scope
C:\Crap\main.cpp:13:17: error: 'Sort' was not declared in this scope
C:\Crap\main.cpp:16:24: error: 'specialSort' was not declared in this scope
C:\Crap\main.cpp:24:33: error: a function-definition is not allowed here before '{' token
C:\Crap\main.cpp:113:1: error: expected '}' at end of input


If I may offer a little advice please. Every coder on this board has been right where you are right now. Looking in the mirror and trying to come to grips with the term "Re-write".
Sometimes it's just easier. The only thing that will help you brother is to get organized.
Write each "Function" on an index card. Read the function back to yourself out loud to make certain they sound right.

Then start again.

1. Pre-Processer commands
2. Prototypes
3. int main() a. Declare your variables
4. int main() b. Write something telling us what the program does.
4. int main() b. call your functions
5. int main() c. handle your business
6. Create your functions.

It happens to the best of us... now it's your turn.

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
105
106
107
108
109
110
111
112
113
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>

int main() 
{
double List[50]={0};             // List is capitalized
int size;
size = readDouble (List);
printf("\n Inverted sum is %lf\n", myInvSum (List, size));
cout << "\n Array before sorting: ";
myPrint (List, size); // call all the functions one by one
Sort (List, size);
cout << "\n Array after sorting: ";
myPrint (List, size);
specialSort (List, size);
cout << "\n Array after special sorting: ";
myPrint (List, size);
printf("\n Inverted sum is %lf\n", myInvSum (List, size));



//double array inverse sum and print
int readDouble(double list[50]) {                       // list is not capitalized
int size = 1, checked;
cout << "\n Enter Array size (maximum 50) ";
cin >> size;
while (size > 50 ) {
cout << "\n Array size cannot be >50, please enter array size less than 50 ";
cin >> size;
} // end while
double item; // array elelment item
for (int i=0; i<size; i++) {
cout << " Enter a double value as array element, zero or non double will stop ";
cin >> item;
if ( item == 0 ) break; // stop upon zero input
// if ( ! (cin >> item) ) { cin.clear(); cin.ignore(1000, '\n'); break ; }
// stop upon non double input
if ( ! cin.eof()) {
checked = cin.peek();
if ( checked == 10 && cin.good()) 
list[i] = item; // store in to the array
else {
cout << "\n Entered a non double, stopping now ";
break;
} // end else
// end if cin eof
// end for
return size;

}
// end readDbl

double myInvSum (double list[50], int size) {
//This function will return the sum 1/x[0]+1/x[1]+...+1/x[size-1]
double sum = 0.0;
for (int i=0; i<size; i++)
sum += 1/list[i]; // invert the number and then add
return sum;

} // end inv sum

void myPrint(double list[50], int size) {
cout << "\n The array elements are: ";
for (int i=0; i<size; i++)
cout << list[i] << " , "; // simply print out to monitor or standard out
} // end myPrint

void Sort (double list[50], int size) { // in ascending order
int j;
double temp;
for (int i=0; i<=size-1; i++) // bubble sort the double array
for (j=i+1; j<size; j++) // inner for loop for j
if ( list[i] > list[j] ) { // then swap
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
} 



void specialSort (double list[50], int size) {
double d1,d2;
int int1, int2, hdi, hdj; 
int j;
double temp;
for (int i=0; i<=size-1; i++)
for (j=i+1; j<size; j++) {

d1 = list[i]; // say d1 = 2.7345
d2 = d1 * 1000.0; // d2 = 2734.5
int1 = d2 / 10; // integer division will give you int1 = 2734.5 / 10 = 273
int2 = int1 / 10 ; // int2 = 273 / 10 = 27
hdi = int1 - int2*10 ; // hd = 273 - 27*10 = 273 - 270 = 3

// and then get hdj next in the same way
d1 = list[j]; // say d1 = 2.6530
d2 = d1 * 1000.0; // d2 = 2653.0
int1 = d2 / 10; // integer division will give you int1 = 2653.0 / 10 = 265
int2 = int1 / 10 ; // int2 = 265 / 10 = 26
hdj = int1 - int2*10 ; // hd = 265 - 26*10 = 265 - 260 = 5

if ( hdi > hdj) { // then swap
temp = list[i];
list[i] = list[j];
list[j] = temp;
{
{ 
} 

return 0; 
}
Last edited on
Topic archived. No new replies allowed.