N
N
nait1233212016-06-18 18:15:59
C++ / C#
nait123321, 2016-06-18 18:15:59

What is wrong with the random question selection code?

Hello, I am doing a coursework, and there you need to do the following: a program for testing. Given 10 questions, among which only 5 should be selected, and added to the testing process itself. Here is my code (I haven't done any validation yet):

#pragma once
#include <stdio.h>
#include <msclr\marshal.h>
#include "header.h" 
#include <cstdlib>
#include <ctime>
#include <iostream>


namespace example {

  using namespace System;
  using namespace System::ComponentModel;
  using namespace System::Collections;
  using namespace System::Windows::Forms;
  using namespace System::Data;
  using namespace System::Drawing;
  using namespace msclr::interop;



  /// <summary>
  /// Сводка для MyForm1
  /// </summary>
  public ref class MyForm1 : public System::Windows::Forms::Form
  {
  public:
    int op = 0;
    MyForm1()
    {
      InitializeComponent();
      //
      //TODO: добавьте код конструктора
      //
    }

  protected:
    /// <summary>
    /// Освободить все используемые ресурсы.
    /// </summary>
    ~MyForm1()
    {
      if (components)
      {
        delete components;
      }
    }

  protected:






  private: System::Windows::Forms::Button^  button2;
  private: System::Windows::Forms::Button^  button3;
  private: System::Windows::Forms::Label^  label1;
  private: System::Windows::Forms::RadioButton^  radioButton1;
  private: System::Windows::Forms::RadioButton^  radioButton2;
  private: System::Windows::Forms::RadioButton^  radioButton3;
  private: System::Windows::Forms::RadioButton^  radioButton4;



  protected:

  private:

    /// <summary>
    /// Требуется переменная конструктора.
    /// </summary>
    System::ComponentModel::Container ^components;



#pragma region Windows Form Designer generated code
    /// <summary>
    /// Обязательный метод для поддержки конструктора - не изменяйте
    /// содержимое данного метода при помощи редактора кода.
    /// </summary>
    void InitializeComponent(void)
    {
      this->button2 = (gcnew System::Windows::Forms::Button());
      this->button3 = (gcnew System::Windows::Forms::Button());
      this->label1 = (gcnew System::Windows::Forms::Label());
      this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
      this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
      this->radioButton3 = (gcnew System::Windows::Forms::RadioButton());
      this->radioButton4 = (gcnew System::Windows::Forms::RadioButton());
      this->SuspendLayout();
      // 
      // button2
      // 
      this->button2->Location = System::Drawing::Point(247, 298);
      this->button2->Margin = System::Windows::Forms::Padding(4, 4, 4, 4);
      this->button2->Name = L"button2";
      this->button2->Size = System::Drawing::Size(100, 28);
      this->button2->TabIndex = 7;
      this->button2->Text = L"<";
      this->button2->UseVisualStyleBackColor = true;
      this->button2->Click += gcnew System::EventHandler(this, &MyForm1::button2_Click);
      // 
      // button3
      // 
      this->button3->Location = System::Drawing::Point(368, 298);
      this->button3->Margin = System::Windows::Forms::Padding(4, 4, 4, 4);
      this->button3->Name = L"button3";
      this->button3->Size = System::Drawing::Size(100, 28);
      this->button3->TabIndex = 8;
      this->button3->Text = L">";
      this->button3->UseVisualStyleBackColor = true;
      this->button3->Click += gcnew System::EventHandler(this, &MyForm1::button3_Click);
      // 
      // label1
      // 
      this->label1->AutoSize = true;
      this->label1->Location = System::Drawing::Point(61, 42);
      this->label1->Margin = System::Windows::Forms::Padding(4, 0, 4, 0);
      this->label1->Name = L"label1";
      this->label1->Size = System::Drawing::Size(70, 17);
      this->label1->TabIndex = 9;
      this->label1->Text = L"Контакти";
      // 
      // radioButton1
      // 
      this->radioButton1->AutoSize = true;
      this->radioButton1->Location = System::Drawing::Point(64, 75);
      this->radioButton1->Name = L"radioButton1";
      this->radioButton1->Size = System::Drawing::Size(110, 21);
      this->radioButton1->TabIndex = 10;
      this->radioButton1->TabStop = true;
      this->radioButton1->Text = L"radioButton1";
      this->radioButton1->UseVisualStyleBackColor = true;
      // 
      // radioButton2
      // 
      this->radioButton2->AutoSize = true;
      this->radioButton2->Location = System::Drawing::Point(64, 123);
      this->radioButton2->Name = L"radioButton2";
      this->radioButton2->Size = System::Drawing::Size(110, 21);
      this->radioButton2->TabIndex = 11;
      this->radioButton2->TabStop = true;
      this->radioButton2->Text = L"radioButton2";
      this->radioButton2->UseVisualStyleBackColor = true;
      // 
      // radioButton3
      // 
      this->radioButton3->AutoSize = true;
      this->radioButton3->Location = System::Drawing::Point(64, 165);
      this->radioButton3->Name = L"radioButton3";
      this->radioButton3->Size = System::Drawing::Size(110, 21);
      this->radioButton3->TabIndex = 12;
      this->radioButton3->TabStop = true;
      this->radioButton3->Text = L"radioButton3";
      this->radioButton3->UseVisualStyleBackColor = true;
      // 
      // radioButton4
      // 
      this->radioButton4->AutoSize = true;
      this->radioButton4->Location = System::Drawing::Point(64, 193);
      this->radioButton4->Name = L"radioButton4";
      this->radioButton4->Size = System::Drawing::Size(110, 21);
      this->radioButton4->TabIndex = 13;
      this->radioButton4->TabStop = true;
      this->radioButton4->Text = L"radioButton4";
      this->radioButton4->UseVisualStyleBackColor = true;
      // 
      // MyForm1
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
      this->ClientSize = System::Drawing::Size(735, 420);
      this->Controls->Add(this->radioButton4);
      this->Controls->Add(this->radioButton3);
      this->Controls->Add(this->radioButton2);
      this->Controls->Add(this->radioButton1);
      this->Controls->Add(this->label1);
      this->Controls->Add(this->button3);
      this->Controls->Add(this->button2);
      this->Margin = System::Windows::Forms::Padding(4, 4, 4, 4);
      this->Name = L"MyForm1";
      this->Text = L"Перегляд контактів";
      this->Load += gcnew System::EventHandler(this, &MyForm1::MyForm1_Load);
      this->ResumeLayout(false);
      this->PerformLayout();

    }



#pragma endregion

    Node *head = NULL; 
    int generateRandNumber()
    {
      srand(time(NULL));
      int a;
      a = (rand() % 10)+1;	
      return a;
    }
    void push_end(S_Data data)
    {
      if (op < 4 && generateRandNumber() > 4)
      {
        Node *NewNode = (Node*)malloc(sizeof(Node)); 
        static Node *tail = NULL;
        
        
          strcpy(NewNode->data.question, data.question); 
          strcpy(NewNode->data.answer1, data.answer1); 
          strcpy(NewNode->data.answer2, data.answer2); 
          strcpy(NewNode->data.answer3, data.answer3);
          strcpy(NewNode->data.answer4, data.answer4);
          strcpy(NewNode->data.answert, data.answert);
          
        
        if (head == NULL) 
        {
          NewNode->next = NULL;
          NewNode->prev = NULL;
          
          head = NewNode;
          tail = NewNode;
          return;
        }
        else
        {
          NewNode->next = NULL;
          tail->next = NewNode;
          NewNode->prev = tail;
          tail = NewNode;
        }
        ++op;
      }

    }

    void print(char c)
    {
      
      if (head != NULL)
      {
        if (c == 'r')//
        {
          if (head->prev != NULL) button2->Enabled = true;  
          else button2->Enabled = false; 
          label1->Text = marshal_as<String^>(head->data.question);
          radioButton1->Text = marshal_as<String^>(head->data.answer1);
          radioButton2->Text = marshal_as<String^>(head->data.answer2);
          radioButton3->Text = marshal_as<String^>(head->data.answer3);
          radioButton4->Text = marshal_as<String^>(head->data.answer4);
          if (head->next != NULL)
          {
            head = head->next;
          }
          else
          {
            button3->Text = "Завершити";
            button3->Enabled = false;
            return;
          }

        }
        else
          
        {
          button3->Enabled = true;//кнопка для руху вперед стає активною

            if (head->prev != NULL)
              head = head->prev; 
            label1->Text = marshal_as<String^>(head->data.question);
            radioButton1->Text = marshal_as<String^>(head->data.answer1);
            radioButton2->Text = marshal_as<String^>(head->data.answer2);
            radioButton3->Text = marshal_as<String^>(head->data.answer3);
            radioButton4->Text = marshal_as<String^>(head->data.answer4);

          
          if (head->prev == NULL) 
          button2->Enabled = false;
          

        }
      }

    }

  private: System::Void MyForm1_Load(System::Object^  sender, System::EventArgs^  e) {


    FILE *file;
    S_Data data;
    file = fopen("kontakt.txt", "r");
    //Считывание инф с файла в список
    fread(&data, sizeof(S_Data), 1, file);

    while (!feof(file)) 
    {
      push_end(data); //Добавление в список
      fread(&data, sizeof(data), 1, file);// Считывание с файлу 1 с вопросами для теста
    }
    fclose(file);

    print('r'); 

  }

  private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {

    print('l'); //Предыдущий вопрос
  }
  private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
    print('r'); //Следующий вопрос
  }
  };
}

The problem is this: each time it either does not read the tests, or the questions go in the usual order. What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abcd0x00, 2016-06-19
@abcd0x00

The problem is this: each time it either does not read the tests, or the questions go in the usual order. What is the problem?

The random number generator is not used in any way. You need to either use it when reading to read a random question, or after reading all the questions to mix the read ones. The first option is better, since the second option will always ask the same questions, albeit in random order. But with the second option, you will have to control the duplication of questions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question