ProgrammingHard
A program multiplies two n x n matrices with n = 500 in int, and the product entries reach about 10^12. What is the error?
- A. There is no error, since matrices never overflow
- B. The matrix must be sorted first
- C. The entries overflow int, so the accumulator must be long long
- D. The product needs a double, which is always exact
Correct answer: C. The entries overflow int, so the accumulator must be long long
A product entry is a sum of 500 products of values near 10^6, so it can exceed INT_MAX. Using long long for the accumulator fixes it.
More C Programming questions
- To answer 'how many times does x appear in a sorted array' in O(log n), which pair of searches is correct?
- A bit array for flags of 10^9 items is held as one byte per item. What is the memory use, and is it fast enough in typical limits?
- struct P { char a; int b; char c; }; printf("%zu", sizeof(struct P)); What is printed with 4-byte int and natural alignment?
- struct P { char a; int b; }; printf("%zu", offsetof(struct P, b)); What is printed with 4-byte int and natural alignment (stddef.h…
- struct R { int v; struct R next; }; struct R c = {3, NULL}, b = {2, &c}, a = {1, &b}; printf("%d", a.next->next->v); What is printed?
- struct Pt { int x, y; }; struct Pt pts[3] = {{1, 2}, {3, 4}, {5, 6}}; struct Pt p = pts + 1; printf("%d %d", p->x, (p - pts)); What is…