Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:09:18+00:00 2026-05-31T06:09:18+00:00

After the enter the first answer the code crashes. Also it states that the

  • 0

After the enter the first answer the code crashes.
Also it states that the memory is unsuccessful allocated. How can i fix this?

#include <stdio.h>
#include <stdlib.h>

#include <string.h>
#include <time.h>

int main(void)
{
  int i;
 srand(time(NULL));
 int *num1;
 int *num2;
 int response;
 int *answer;
 char *result;

 printf("\nMath Quiz\n");
 printf("Enter # of problems: ");
 scanf("%d", &response);

based on the number of questions the user wishes to take, allocate enough memory to hold question data

num1 = (int *)calloc(response, sizeof(int));
num2 = (int *)calloc(response, sizeof(int));
answer = (int *)calloc(response, sizeof(int));
result - (char *)calloc(response, sizeof(char));

if(num1 == NULL || num2 == NULL || answer == NULL || result == NULL)
{
printf("memory allocation unsucessful\n");
}  //end if

for(i=0; i<response; i++)
{

num1[i] = (rand() % 12)+1;
num2[i] = (rand() % 12)+1;

printf("%d * %d = ", num1[i], num2[i]); //somewhere at this point the program messes up
scanf("%d", &answer[i]);

if(answer[i]= num1[i] * num2[i])
{
    result[i] = 'c';
}

else
{
    result[i] = 'i';
}  
}  //end for loop

 printf("Quiz Results\n");
 printf("Question\tYour Answer\tCorrect");

 for(i=0; i<response; i++);
 {
          if(result[i] == 'c')
          {
                        printf("%d * %d\t\t%d\t\tYES",num1[i],num2[i],answer[i]);
          }

          else
          {
                        printf("%d * %d\t\t%d\t\tNo",num1[i],num2[i],answer[i]);
        }
} //end for loop      



free(num1);
free(num2);
free(answer);
free(result);

system("pause");
return 0;

} //end main

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:09:20+00:00Added an answer on May 31, 2026 at 6:09 am

    answer[i]= num1[i] * num2[i]

    should read

    answer[i] == num1[i] * num2[i]

    = is for assignments, == is for comparisons.

    and result - (char *)calloc(response, sizeof(char));

    should read

    result = (char *)calloc(response, sizeof(char));

    If there are other problems, you need to be more specific than “the program messes up”.

    Also, don’t cast the return value of malloc or calloc. Read Do I cast the result of malloc? .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After loosing much sleep I still cannot figure this out: The code below (its
After answering this question and not finding a satisfying answer in the standard paper,
I have been searching all over but I can't find any answer to this.
i have written a small piece of code. This code first blocks the {SIGSEGV},
I can't seem to figure out why this falls into a loop after getting
I have a gtk.TextBuffer which is supposed to be cleared after pressing Enter, similar
A colleague suggested to the testers they enter javascript:resizeTo(1024,768); into the address bar after
I have a 2D dynamic array. I enter a line of 0's after line
first StackOverflow question. I'm writing a predicate in prolog that takes three parameters. These
this is my code about bubble sort. #include stdafx.h #include <iostream> #include <cstdlib> using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.