Please Help Me

Pages: 12
.
Last edited on
I need basic c++ codes.
Why are you using printf and scanf? For C++, you should use cout and cin.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// This progam calculates the user's pay.
#include <iostream>
using namespace std;

int main()
{
   double hours, rate, pay;

   // Get the number of hours worked.
   cout << "How many hours did you work? ";
   cin >> hours;
   
   // Get the hourly pay rate.
   cout << "How much do you get paid per hour? ";
   cin >> rate;
   
   // Calculate the pay.
   pay = hours * rate;
   
   // Display the pay.
   cout << "You have earned $" << pay << endl;
   return 0; 
}


let me explain.
Please, do explain. What problems are you having with the program? Be specific about it. If you provide vague explanations, you will receive vague answers (if any).

I know very simple but i can't do it.
The word simple is a relative term. Yes it is simple for some people, but not everyone. If you can't do it, then it is not simple. Isn't it? If this is a homework assignment, then the expectation is that you know the basics of the C++ language. Then again, since it is simple, you should be able to explain what is the problem with the code.

I hope it helps.
yea you are true, i need it for homework.
should i use array and FILE for save students?
first question (how many student you have?) is it my array size? so i should use const?
..
Last edited on
Instead of malloc, you should use the new and delete operator if you must dynamically allocate memory.
Here is an example of how to dynamically allocate memory using the new/delete operator:
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
int main()
{
    double *sales, // To dynamically allocate an array
    total = 0.0,   // Accumulator
    average;       // To hold average sales
    int numDays,   // To hold the number of days of sales
        count;     // Counter variable


    // Get the number of days of sales.
    cout << "How many days of sales figures do you wish ";
    cout << "to process? ";
    cin >> numDays;

    // Dynamically allocate an array large enough to hold
    // that many days of sales amounts.
    sales = new double[numDays];

    // Get the sales figures for each day.
    cout << "Enter the sales figures below.\n";
    for (count = 0; count < numDays; count++)
    {
    cout << "Day " << (count + 1) << ": ";
    cin >> sales[count];
    }
    // Free dynamically allocated memory
    delete [] sales;
    sales = nullptr; // Make sales point to null.

    return 0;


This is by assuming that you must use C++ code.
Last edited on
thank you. this code help a lot..
now i need turn menu after 1 student register
and i need search can u explain?

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
#include <iostream>
#include <stdio.h>
using namespace std;

int main ()
 {
int *array;
int i,total,num,grade,ch;
cout <<"How many....: ";
cin>>total;
array= new int[total];

cout <<"1 - New \n2 - grade \n3 - list \n  : ";
cin>>ch;

switch(ch)
{
case 1:
for (i=0;i<total;i++)
{
cout <<"number :";
cin >> num;
cout << "grade :";
cin >> grade;
}

break;
}

return 0;
}
You may use a switch statements for menus.
For example:
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
   // Display the menu and get a choice.
   cout << "\t\tHealth Club Membership Menu\n\n"
        << "1. Standard Adult Membership\n"
        << "2. Child Membership\n"
        << "3. Senior Citizen Membership\n"
        << "4. Quit the Program\n\n"
        << "Enter your choice: ";
   cin >> choice;
   
   // Respond to the user's menu selection.
   switch (choice)
   {
      case 1:
         cout << "For how many months? ";
         cin >> months;
         charges = months * ADULT;
         cout << "The total charges are $" << charges << endl;
         break;
         
      case 2:
         cout << "For how many months? ";
         cin >> months;
         charges = months * CHILD;
         cout << "The total charges are $" << charges << endl;
         break;
         
      case 3:
         cout << "For how many months? ";
         cin >> months;
         charges = months * SENIOR;
         cout << "The total charges are $" << charges << endl;
         break;
        
      case 4:
         cout << "Program ending.\n";
         break;
         
      default:
         cout << "The valid choices are 1 through 4. Run the\n"
              << "program again and select one of those.\n";
   }


Have you learned about linear search and binary search in the array?
Last edited on
u saw my codes, i ll write case 2 for search, case 3 for list all as my first entry user should return menu after every step. when user enter array size for going number of times. i want turn 1 time and go menu. in ur last code default how go menu?

we learned search but not have many practise if u give me hint i can solve i think.

by the way really thanks for help i won't forget this (sorry for bad english)
in the switch example code, the default is to let the user know that the user did not enter the correct choice.

The linear search is a very simple algorithm. Sometimes called a sequential search , it uses
a loop to sequentially step through an array, starting with the first element. It compares
each element with the value being searched for and stops when either the value is found or
the end of the array is encountered. If the value being searched for is not in the array, the
algorithm will unsuccessfully search to the end of the array. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int searchList(const int list[], int numElems, int value)
{
int index = 0; // Used as a subscript to search array
int position = −1; // To record position of search value
bool found = false; // Flag to indicate if the value was found
while (index < numElems && !found)
{
if (list[index] == value) // If the value is found
{
found = true; // Set the flag
position = index; // Record the value's subscript
}
index++; // Go to the next element
}
return position; // Return the position, or −1
}


The binary search is a clever algorithm that is much more efficient than the linear search.
Its only requirement is that the values in the array be sorted in order. Instead of testing the
array’s first element, this algorithm starts with the element in the middle. If that element happens
to contain the desired value, then the search is over.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int binarySearch(const int array[], int numElems, int value)
{
int first = 0, // First array element
last = numElems − 1, // Last array element
middle, // Midpoint of search
position = −1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate midpoint
if (array[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle − 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}

when i use binary search, how can i write?

cout << search_number
cout << value => give that student's grade?

and how can i turn menu every step? for example;

1. For new student
2. for see grade
3. for list all
1 =
Student Number :1
Student Grade : 2
1. For new student
2. for see grade
3. for list all
1=
Student Number :2
Student Grade : 5
1. For new student
2. for see grade
3. for list all
1=
Student Number :4
Student Grade : 3
1. For new student
2. for see grade
3. for list all
1=


i have 1 problem too.. program giving back same results.. for example

input
1 - 3
2 - 4
3- 5

program giving out
3-5
3-5
3-5

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
#include <iostream>
#include <stdio.h>
using namespace std;

int main ()
 {
int *array;
int i,toplam,ono, secim,onot,exit=1;
cout <<"Öğrenci Sayısını Giriniz: ";
cin>>toplam;
array= new int[toplam];
while (exit == 1)
{
cout <<"\n1 - Not Ver \n2 - Not Gör \n3 - Tum Ogrencilerin Notlari\n4 - Cikis\n Seciminiz : ";
cin>>secim;
switch(secim)
{
case 1:
for (i=0;i<toplam;i++)
{
cout <<"Ogrenci No :";
cin >> ono;
cout << "Ogrenci Not :";
cin >> onot;
}

break;
case 2:
cout<<"Ogrenci No :";
break;
case 3:
for (i=0;i<toplam;i++)
{
cout<<"\n   "<<ono <<"-"<<onot ;
//cout<<"\nOgrenci Not:"<<onot;
}
break;

}
}
//return 0;
}


Would it be possible to post the homework question? That way we could understand what the program is supposed to do.
Write a program to keep students' final grades.
School number of students starting at 1 until N assumes that you think. To be written
The two most important programs do; search and new entry
to list the notes. Note: You must use a 1D array. Between 0 and 10 in the Student Notes

Running the sample program:
main ./
Enter the number o Students: 10
O Students give notes on 1, to learn the notes on 2, all of the students
To view the notes to 3: 1
Ø Student No. 4
Ø Student notes: 4
O Students give notes on 1, to learn the notes on 2, all of the students
To view the notes to 3: 1
Ø Student No. 3
Ø Student notes: 10
O Students give notes on 1, to learn the notes on 2, all of the students
To view the notes to 3: 3
1-0
2-0
3-10
4-4
5-0
6-0
7-0
8-0
9-0
10-0
O Students give notes on 1, to learn the notes on 2, all of the students
To view the notes to 3: 2
Ø Student No. 6
Ø Student notes 0.
O Students give notes on 1, to learn the notes on 2, all of the students
To view the notes to 3: 2
Ø Student No. 11
No student with this student number on the list.
O Students give notes on 1, to learn the notes on 2, all of the students
To view the notes to 3:
...
my array has problem i think..

cout << "student number :"
cin>>ono (may i use ono[i]? )
i fixed array problem.
to insert a number in an array,

1
2
3
4
for(int i = 0; i < sizeofArray; i++)
{
    cin >> ono[i];
}
Last edited on
..
Last edited on
Just remember for a binary search, the requirement is that the values in the array be sorted in order. For the linear search the values in the array does not have to be in order.

Last edited on
can u give me example for linear search?
Pages: 12